Python爬蟲實戰,requests+parsel模塊,爬取安居客二手房房源信息數據

前言

本文給大家分享的是如何通過Python爬蟲採集安居客二手房房源信息數據。

開發工具

Python版本: 3.8

相關模塊:

requests模塊
parsel模塊

環境搭建

安裝Python並添加到環境變量,pip安裝需要的相關模塊即可。

思路分析

本文以爬蟲安居客二手房源信息,講解如何採集安居客二手房源數據

要爬取房源如下圖所示:

提取頁面數據

瀏覽器中打開我們要爬取的頁面
按F12進入開發者工具,查看我們想要的數據在哪裏
這裏我們需要安居客房源頁面數據就可以了

代碼實現

# 僞裝
headers = {
    'cookie': 'aQQ_ajkguid=B7A0A0B5-30EC-7A66-7500-D8055BFFE0FA; ctid=27; id58=CpQCJ2Lbhlm+lyRwdY5QAg==; _ga=GA1.2.2086942850.1658553946; wmda_new_uuid=1; wmda_uuid=009620ee2a2138d3bd861c92362a5d28; wmda_visited_projects=%3B6289197098934; 58tj_uuid=8fd994c2-35cc-405f-b671-2c1e51aa100c; als=0; ajk-appVersion=; sessid=8D76CC93-E1C8-4792-9703-F864FF755D63; xxzl_cid=2e5a66fa054e4134a15bc3f5b47ba3ab; xzuid=e60596c8-8985-4ab3-a5df-90a202b196a3; fzq_h=4c8d83ace17a19ee94e55d91124e7439_1666957662955_85c23dcb9b084efdbc4ac519c0276b68_2936029006; fzq_js_anjuke_ershoufang_pc=75684287c0be96cac08d04f4d6cc6d09_1666957664522_25; twe=2; xxzl_cid=2e5a66fa054e4134a15bc3f5b47ba3ab; xxzl_deviceid=OOpJsA5XrQMdJFfv71dg+l+he0O1OKPQgRAQcFPbeRAyhjZ4/7gS3Gj4DfiLjxfc; isp=true; obtain_by=2; new_session=1; init_refer=https%253A%252F%252Fcs.anjuke.com%252F; new_uv=3',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
1.發送請求
response = requests.get(url=url, headers=headers)
2.獲取數據
html_data = response.text
3.解析數據
 select = parsel.Selector(html_data)
    divs = select.css('.property-content')
    for div in divs:
        # .property-content-title-name   標題
        標題 = is_null(div.css('.property-content-title-name::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(1) span  戶型
        戶型s = div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(1) span::text').getall()
        戶型 = ' '.join(戶型s)
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(2)  面積
        面積 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(2)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(3)  朝向
        朝向 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(3)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(4)  樓層
        樓層 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(4)::text').get())
        # .property-content-info:nth-child(1) .property-content-info-text:nth-child(5)  年份
        年份 = is_null(div.css('.property-content-info:nth-child(1) .property-content-info-text:nth-child(5)::text').get())
        # .property-content-info:nth-child(2) .property-content-info-comm-name  小區名稱
        小區名稱 = is_null(div.css('.property-content-info:nth-child(2) .property-content-info-comm-name::text').get())
        # .property-content-info:nth-child(2) .property-content-info-comm-address  小區地址
        小區地址 = is_null(div.css('.property-content-info:nth-child(2) .property-content-info-comm-address::text').get())
        # .property-content-info:nth-child(3) span  小區標籤
        小區標籤s = div.css('.property-content-info:nth-child(3) span::text').getall()
        小區標籤 = ' '.join(小區標籤s)
        # .property-price .property-price-total .property-price-total-num  總價
        總價 = is_null(div.css('.property-price .property-price-total .property-price-total-num::text').get())
        # .property-price .property-price-average  每平方米的價格
        單價 = is_null(div.css('.property-price .property-price-average::text').get())
        print(標題, 戶型, 面積, 朝向, 樓層, 年份, 小區名稱, 小區地址, 小區標籤, 總價, 單價)
4.保存數據
        with open('安居客.csv', mode='a', encoding='utf-8', newline='') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow([標題, 戶型, 面積, 朝向, 樓層, 年份, 小區名稱, 小區地址, 小區標籤, 總價, 單價])

結果展示

\color{red}{ps:}圖片僅供參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章