zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python采集本地二手房,一键知晓上万房源信息

Python 信息 本地 一键 采集 知晓
2023-09-14 09:05:34 时间

前言

大家早好、午好、晚好吖 ❤ ~欢迎光临本文章

所以今天教大家用Python来采集本地房源数据,帮助大家筛选好房。

话不多说,让我们开始愉快的旅程吧~


更多精彩内容、资源皆可点击文章下方名片获取此处跳转


本文涉及知识点

  1. 采集基本流程

  2. requests 发送请求

  3. parsel 解析数据

  4. csv 保存数据

第三方模块

  • requests >>> pip install requests 数据请求

  • parsel >>> pip install parsel 解析模块 (提取数据)

如果安装python第三方模块:

  1. win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车

  2. 在pycharm中点击Terminal(终端) 输入安装命令


开发环境

Python 3.8

Pycharm


本文思路

一.、思路分析

找到数据来源

二、代码实现

发送请求 & 获取数据

解析数据 (提取你想要的数据)

保存数据


代码展示

使用的模块

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'
}
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[2]/text()").get().strip()
    direction = div.xpath(".//div[@class='property-content-info']/p[3]/text()").get().strip()
    floor = div.xpath(".//div[@class='property-content-info']/p[4]/text()").get("").strip()
    year = div.xpath(".//div[@class='property-content-info']/p[5]/text()").get("").strip()
    price_total = ''.join(div.xpath(".//div[@class='property-price']/p[1]/span/text()").getall()).strip()
    price_average = div.xpath(".//div[@class='property-price']/p[2]/text()").get().strip()
    print(title, house_type, area, direction, floor, year, price_total, price_average)

保存数据

csv_writer.writerow([title, house_type, area, direction, floor, year, price_total, price_average])

尾语 💝

好了,今天的分享就差不多到这里了!

完整代码、更多资源、疑惑解答直接点击下方名片自取即可。

对下一篇大家想看什么,可在评论区留言哦!看到我会更新哒(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

最后,宣传一下呀~👇👇👇更多源码、资料、素材、解答、交流皆点击下方名片获取呀👇👇👇