from selenium import webdriver # 下载的geckodriver的存储位置 driver = webdriver.Chrome(executable_path=‘D:\\chromedriver.exe‘) # 自动访问的网站 driver.get("https://www.airbnb.cn/s/Shenzhen--China/homes") #找到页面中所有的出租房 rent_list = driver.find_elements_by_css_selector(‘div._gig1e7‘) #每一个出租房的评论数量 for eachhouse in rent_list: #找出评论数量 try: comment = eachhouse.find_element_by_css_selector(‘span._1clmxfj‘) comment = comment.text except: comment = 0 #找到价格 price = eachhouse.find_element_by_css_selector(‘div._1ixtnfc‘) price = price.text #.replace("每晚","").replace("价格","").replace("\n","") #找到名称 name = eachhouse.find_element_by_css_selector(‘div._qrfr9x5‘) name = name.text #找到房屋种类 details = eachhouse.find_element_by_css_selector(‘div._1dir9an‘) details = details.text print(comment,price,name,details)
重点是通过【检查】来查看元素的定位名称,根据类型选择对应函数。
原文:https://www.cnblogs.com/hsk832/p/13965007.html