Python爬蟲: 爬取淮安出租房源信息56頁1111套

Python爬蟲:

# 爬取淮安出租房源信息1111套
# 爬取內容爲小區名、戶型、面積、價格、地址
# 本次爬取使用xpath進行數據的提取

1、導入模塊
   

    import requests
    from lxml import etree
    import threading

 2、定義huaian_chuzu_house(i)函數進行頁面爬取

def huaian_chuzu_house(i):
    url = 'https://m.lianjia.com/chuzu/ha/zufang/pg%s/?ajax=1' % i
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
        '537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/'
        '537.36 Core/1.63.6788.400 QQBrowser/10.3.2727.400',
        'Referer': 'https://m.lianjia.com/ha/',
        'Upgrade-Insecure-Requests': '1'
    }

    response = requests.get(url=url, headers=headers)
    html = response.text
   

3、解析頁面 

    info_html = etree.HTML(html)
    xiaoqus = info_html.xpath('//div[@class="content__item__main"]')
    for xiaoqu in xiaoqus:
        # 先獲取一套房源的信息,包括小區名、朝向、面積、地址、價格
        name = xiaoqu.xpath('.//p[@class="content__item__title"]/text()')[0].strip()
        area = xiaoqu.xpath('.//p[@class="content__item__content"]/text()')[0].strip().split('\n')[0].strip()
        address = xiaoqu.xpath('.//p[@class="content__item__content"]/text()')[0].strip().split('\n')[2].strip()
        price = xiaoqu.xpath('.//p[@class="content__item__bottom"]/text()')[0].strip()
        # 定義字典保存信息
        rets = {
            "戶型": name,
            "面積": area,
            "地址": address,
            "價格元/月": price
        }
        # 打印信息
        print(rets)

4、創建主函數 

def main():
    for i in range(56):
        # 創建一個線程
        pl = threading.Thread(huaian_chuzu_house(i))
        pl.start()
        print(i)

5、對函數調用運行 

if __name__ == '__main__':
    main()

6、結果截圖

 

發佈了42 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章