利用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
相关推荐
TTGGGFF1 分钟前
实用代码工具:Python打造PDF选区OCR / 截图批量处理工具(支持手动/全自动模式)
python·pdf·ocr
山峰哥1 小时前
Python爬虫实战:从零构建高效数据采集系统
开发语言·数据库·爬虫·python·性能优化·架构
Jay_Franklin8 小时前
SRIM通过python计算dap
开发语言·python
是一个Bug8 小时前
Java基础50道经典面试题(四)
java·windows·python
吴佳浩9 小时前
Python入门指南(七) - YOLO检测API进阶实战
人工智能·后端·python
liliangcsdn9 小时前
python下载并转存http文件链接的示例
开发语言·python
大、男人10 小时前
python之Starlette
python·uvicorn
小白学大数据11 小时前
Java 爬虫对百科词条分类信息的抓取与处理
java·开发语言·爬虫
小智RE0-走在路上12 小时前
Python学习笔记(11) --数据可视化
笔记·python·学习
历程里程碑12 小时前
hot 206
java·开发语言·数据结构·c++·python·算法·排序算法