利用python爬取某壳的房产数据

以无锡的某壳为例进行数据爬取,现在房子的价格起伏很快,买房是人生一个大事,了解本地的房价走势来判断是否应该入手。

(建议是近2年不买,本人在21年高位抛了一套房,基本是通过贝壳数据判断房价已经到顶,希望此爬虫能够帮到各位。)

这里只爬了必看好房的数据,贝壳有放抓机制,无法跑全所有数据,有心的可以拿过去扩展一番。

复制代码
import requests
from pyquery import PyQuery as pq
import json
import pandas as pd
import datetime,time
columns = ['id','title','place','msg', 'price', 'per_meter','area','city']
areas=['滨湖区','梁溪区','新吴区','惠山区','锡山区','江阴市','宜兴市']
# 爬取某网页
def get_a_page(url,area):
    result = requests.get(url)
    doc = pq(result.text)
    ul = doc('.sellListContent')
    divs = ul.children('.clear .info.clear').items()
    count = 0
    realids=[]
    titles = []
    places = []
    msgs = []
    prices = []
    per_meters = []
    realarea=[]
    citys=[]
    for div in divs:
        count += 1
        realid=div.children('.address .priceInfo .unitPrice').attr('data-hid')
        title = div.children('.title a').text()
        place = div.children('.address .flood .positionInfo a').text()
        msg = div.children('.address .houseInfo').text()
        price = div.children('.address .priceInfo .totalPrice span').text()
        per_meter = div.children('.address .priceInfo .unitPrice span').text()
        city='无锡'
        dict = {
            'id':realid,
            'title': title,
            'place': place,
            'msg': msg,
            'price': price,
            'per_meter': per_meter,
            'area':areas[area],
            'city':'无锡'
        }
        realids.append(realid)
        titles.append(title)
        places.append(place)
        msgs.append(msg)
        prices.append(price)
        per_meters.append(per_meter)
        realarea.append(areas[area])
        citys.append(city)
        print(str(count) + ':' + json.dumps(dict, ensure_ascii=False))
    datas={
        'id':realids,
        'title': titles,
        'place': places,
        'msg': msgs,
        'price': prices,
        'per_meter': per_meters,
        'area':realarea,
        'city':citys
    }
    df = pd.DataFrame(data=datas, columns=columns)
    df.to_csv('wx'+time.strftime('%Y-%m-%d')+'.csv', mode='a', index=False, header=False)

if __name__ == '__main__':
    quyu=['binhu','liangxi','xinwu','huishan','xishan','jiangyinshi','yixingshi']
    index=0
    for qy in quyu:
        #print (index)
        #print (qy)
        for i in range(1, 20):
            get_a_page(f'https://wx.ke.com/ershoufang/{qy}/pg{i}tt9/',index)
        index=index+1
相关推荐
爱上纯净的蓝天3 小时前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
cspttty3 小时前
2027风险管理岗准备路线:数学与应用数学如何补齐金融、SQL与Python
开发语言·python·金融
用户8356290780513 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python
我命由我123453 小时前
Python 开发 - Python 的 pathlib
java·开发语言·后端·python·学习·java-ee·学习方法
严谨的麻辣烫4 小时前
Python HTTP 连接池怎么用?从连接复用到请求异常排查
开发语言·python·http·requests·网络排查
摇滚侠4 小时前
《Java并发编程之美》第 4 章 Java 并发包中原子操作类原理剖析 阅读笔记 7
java·笔记·python
用户8356290780514 小时前
如何使用 Python 在 Word 文档中创建和格式化列表
后端·python
GIoT80104 小时前
网络出口如何选择?固定 IP 与 IP 轮换的原理及实践
python
羡林i5 小时前
ProcessPoolExecutor 传参踩坑实测:pickle 失败的四种写法
python
Dmz丶5 小时前
前端转 AI 100 天 Day 30:正式上线——从零部署一个完整服务(含 30 天复盘)
python