血面纱 发表于 2023-3-1 21:42:43

Python爬取安居客房源信息,轻松获取优质房源!

又到了所谓的金山银四就业季,那找工作的小伙伴宿舍住不惯的话,就会去租房子住,当然也不一定有宿舍,那么自己找房子的话,肯定是不知道哪哪好。
所以今天教大家用Python来采集本地房源数据,帮助大家筛选好房。

本文涉及知识点

[*]爬虫基本流程
[*]requests 发送请求
[*]parsel 解析数据
[*]csv 保存数据
开发环境
Python 3.8
Pycharm 
本文思路
一.、思路分析
找到数据来源
https://cs.anjuke.com/sale/p1/?from=navigation
二、代码实现

[*]发送请求 & 获取数据
[*]解析数据 (提取你想要的数据)
[*]保存数据
代码展示
使用的模块
import requests
import parsel
import csv 
发送请求 & 获取数据
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
}
# 为了防止大家看不懂,我特地录制了视频详细讲解,包括完整代码都打包好了。
# 我还准备了数百本电子书,大量的视频教程,直接在这个Q裙:708525271 加它自取。
response = requests.get(url, headers=headers, proxies=get_proxies())
html_data = response.text 
解析数据
selector = parsel.Selector(html_data)
divs = selector.xpath("//div[@class='property']")
for div in divs:
    # 单个房源去提取
    title = div.xpath(".//div[@class='property-content']//h3/text()").get().strip()
    house_type = ''.join(div.xpath(".//div[@class='property-content-info']//span/text()").getall()).strip()
    area = div.xpath(".//div[@class='property-content-info']/p/text()").get().strip()
    direction = div.xpath(".//div[@class='property-content-info']/p/text()").get().strip()
    floor = div.xpath(".//div[@class='property-content-info']/p/text()").get("").strip()
    year = div.xpath(".//div[@class='property-content-info']/p/text()").get("").strip()
    price_total = ''.join(div.xpath(".//div[@class='property-price']/p/span/text()").getall()).strip()
    price_average = div.xpath(".//div[@class='property-price']/p/text()").get().strip()
    print(title, house_type, area, direction, floor, year, price_total, price_average) 
保存数据
csv_writer.writerow() 
好了,今天的分享就到这里结束了,兄弟们快去试试吧!

来源:https://www.cnblogs.com/hahaa/p/17169435.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Python爬取安居客房源信息,轻松获取优质房源!