bs4+phantomjs爬取安居客二手房信息

bs4+phantomjs爬取安居客二手房信息

这是我的第一篇博客,希望通过养成写博客的习惯来督促自己学习。

开发环境以及需要安装的模块
- Python3.6
- requests pip install requests
- BeautifulSoup4 pip install BeautifulSoup4
- selenium pip install selenium
- phantomjs 官网http://phantomjs.org/


分析

1、先来看下安居客移动端页面https://m.anjuke.com/sh/sale/pudong/?from=filterarea
2、点击 浦东 , 弹出菜单,左边是区,后边是商圈。
3、不停下拉直至出现,没有更多这里写图片描述
我们用phantomjs来模拟浏览器操作。
需要注意的点:
某些商圈没有二手房信息,会在页面上显示 “没有符合条件的房源,请修改条件或关键词”。如青浦区下面的青东农场。

步骤

以安居客上海二手房为例。
为了尽量爬取更多的信息,以区为单位爬取。
以浦东为例:
- 获取浦东地区所有商圈的链接
- 对于每个商圈获取其页面
- 下拉至出现 没有更多
- 下载其HTML代码
- 利用bs4分析获取item
- 以json格式写入文件
如何获得每个区的链接我就不说了。
如和使用bs4我也不说了。

项目结构

Anjuke
—— anjuke.py
—— config.py

代码块

以下anjuke.py是部分代码

'''
    step-1.获取所有商圈的移动端页面
    step-2.每个页面模拟下拉直至完全显示
    step-3.解析页面获得items
    step-4.将items写入文件
'''
def main():
    # init
    start_url = 'https://m.anjuke.com/sh/sale/pudong/?from=filterarea'
    browser = webdriver.PhantomJS('E:\\phantomjs\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
    #browser = webdriver.Firefox()
    browser.start_session(dynamic_headers())      # 设置动态头部
    browser.get(start_url)
    # step-1
    for url in get_business_districts_url(browser.page_source):
        # step-2
        browser.start_session(dynamic_headers())
        browser.implicitly_wait(5)
        browser.set_page_load_timeout(10)
        browser.set_script_timeout(10)
        browser.get(url)
        count = 0
        log('正在爬取 %s' %url)
        # 这里加个try/catch, 因为某些商圈是没有二手房信息
        try:
            if browser.find_element_by_class_name('noresult'):
                log('没有符合条件的房源!')
                log('爬取 %s 结束' % url)
                continue
        except NoSuchElementException:
            pass
        while True:
            time.sleep(random.randint(2, 4))            # 每次下拉后等待
            nomore = browser.find_element_by_id('nomore')   # 没有更多
            if nomore.get_attribute('style') != 'display: block;':
                browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
                count += 1
                log('下拉 %d 次' %count)
            else:
                break
                # step-3, step-4
        for house_item in parse(browser.page_source):
            save(house_item)
        log('爬取 %s 结束' %url)
        time.sleep(random.randint(3, 5))

'''
    得到商圈的链接
'''
def get_business_districts_url(html):
    soup = BeautifulSoup(html, 'lxml')
    districts = soup.select('#rsswitchinfo .blockinfo .blocklist a')
    for tag_a in districts:
        tag_a_text = tag_a.get_text().strip()
        if(tag_a_text != '全部'):
            yield tag_a.attrs['href']


'''
    动态headers配置, 设置User-Agent和Proxy
'''
def dynamic_headers():
    dc = DesiredCapabilities.PHANTOMJS.copy()
    dc['phantomjs.page.settings.userAgent'] = random.choice(MOBILE_USER_AGENTS)
    dc["phantomjs.page.settings.loadImages"] = False
    '''
    proxy = webdriver.Proxy()
    proxy.proxy_type = ProxyType.MANUAL
    proxy.http_proxy = ''
    proxy.add_to_capabilities(dc)
    '''
    return dc


def save(content):
    with open('result3.txt', 'a', encoding='utf-8') as file:
        line = json.dumps(dict(content), ensure_ascii=False) + '\n'
        file.write(line)
        file.close()

以下config.py是部分代码

MOBILE_USER_AGENTS=[
    'Mozilla/5.0 (Linux; U; Android 5.1; zh-cn; m1 metal Build/LMY47I) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.6 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 5.1.1; vivo X7 Build/LMY47V; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.116 Mobile Safari/537.36 baiduboxapp/8.6.5 (Baidu; P1 5.1.1)',
    'Mozilla/5.0 (iPhone 6s; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.6.0 Mobile/14E304 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1',
    'Mozilla/5.0 (iPhone 6s; CPU iPhone OS 9_3_5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 MQQBrowser/7.5.1 Mobile/13G36 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1',
    'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; Redmi 3X Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.2 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; HUAWEI MT7-TL00 Build/HuaweiMT7-TL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.3.8.909 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-CN; F5121 Build/34.0.A.1.247) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.5.1.944 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 7.0; zh-CN; VIE-AL10 Build/HUAWEIVIE-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.4.2.936 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; MI 5 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.1 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; SM-C5000 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.5 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; MI NOTE LTE Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.146 Mobile Safari/537.36 XiaoMi/MiuiBrowser/8.8.7',
    'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-cn; vivo X9 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.6 Mobile Safari/537.36',
    'Mozilla/5.0 (iPhone 6; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.6.0 Mobile/14E277 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1'
]

你可能感兴趣的:(爬虫)