selenium抓取Airbnb深圳短租数据

1、网站分析

    首先我们需要打开Airbnb深圳前200短租房源的网页(https://zh.airbnb.com/s/Shenzhen-China/homes?refinement_paths%5B%5D=%2Fhomes&allow_override%5B%5D=&s_tag=GAE-MLbZ),然后点击邮件打开“检查”,如下图所示:


我们最开始需要做的就是查找出我们需要抓取的date,首先需要找到这些数据所在的定位div._v721rv;其次是找出我们需要的数据所在的各个小的标签模块:


这个是房型的定位标签:span,也许一次找不对我们需要定位的数据,可以慢慢去找,再次就是店铺名称及其位置:


上图显示的div._177djt7om就是定位标签,其他价格和评论都是一样的就给附上照片:


上面的都是首先需要找出来的数据定位。

扫描二维码关注公众号,回复: 2506064 查看本文章

2、然后代码实现:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

caps = webdriver.DesiredCapabilities.FIREFOX
caps['marionette'] = False
binary = FirefoxBinary(r'F:\Program Files (x86)\Mozilla Firefox\firefox.exe')
fp = webdriver.FirefoxProfile()
fp = fp.set_preference("permissions.default.stylesheet",2)
driver = webdriver.Firefox(firefox_binary=binary,firefox_profile=fp,capabilities=caps)
# driver = webdriver.Chrome()
for i in range(20):
    driver.get("https://zh.airbnb.com/s/Shenzhen-China/homes?refinement_paths%5B%5D=%2Fhomes&allow_override%5B%5D=&s_tag=_45KiJet§ion_offset="+str(i))
    rent_list = driver.find_elements_by_css_selector("div._v72lrv")
    for eachhouse in rent_list:
        comment = eachhouse.find_element_by_css_selector("div._17djt7om")
        comment = comment.text
        price = eachhouse.find_element_by_css_selector("div._59f9ic")
        price = price.text[4:]
        fangxin = eachhouse.find_element_by_tag_name('span')
        fangxin = fangxin.text
        evaluate = eachhouse.find_element_by_css_selector("div._1hc6xcl")
        evaluate = evaluate.text
        print("评论"+evaluate+",价格"+price,fangxin,comment+"\n")

在这段代码中我们可能会遇到的问题就是去如何定位到需要爬取的数据的位置,这个是最难的但是这个需要自己去尝试。

       我刚开始爬去这个页面的数据的时候就一直给我提示没有发现相应的位置,这个网页的评论如果没有的话就会出现没有span标签但是我们可以从这个标枪的上一级位置开始查找,没有这个应该会有他的父级标签定位,这样就不会出现中途出错了。

猜你喜欢

转载自blog.csdn.net/qq_35490191/article/details/79750492