爬取链家二手房数据

简介: 爬取链家二手房数据
#coding:utf8
import requests
import random
from lxml import etree
import time
#提供ua信息的的包
from fake_useragent import UserAgent
class LinajiaSpider(object):
    def __init__(self):
        self.url='https://bj.lianjia.com/ershoufang/pg{}/'
        #计数,请求一个页面的次数,初始值为1
        self.blog=1
    # 随机取一个UA
    def get_header(self):
        #实例化ua对象
        ua=UserAgent()
        headers={
   'User-Agent':ua.random}
        return headers
    #发送请求
    def get_html(self,url):  
       #在超时间内,对于失败页面尝试请求三次
        if self.blog<=3:
            try:
                res=requests.get(url=url,headers=self.get_header(),timeout=3)
                html=res.text
                return html
            except Exception as e:
                print(e)
                self.blog+=1
                self.get_html(url)
    # 解析提取数据
    def parse_html(self,url):
        html=self.get_html(url)
        if html:
            p=etree.HTML(html)
            #基准xpath表达式-30个房源节点对象列表
            h_list=p.xpath('//ul[@class="sellListContent"]/li[@class="clear LOGVIEWDATA LOGCLICKDATA"]')
            #所有列表节点对象
            for h in h_list:
                item={
   }
                #名称
                name_list=h.xpath('.//a[@data-el="region"]/text()')
                #判断列表是否为空
                item['name']=name_list[0] if name_list else None
    #户型+面积+方位+是否精装..['2室1厅 | 88.62平米 | 北 南 | 简装 | 顶层(共6层) | 2004年建 | 板楼']
                info_list=h.xpath('.//div[@class="houseInfo"]/text()')
                #判断列表是否为空
                if info_list:
                    L=info_list[0].split('|')
          # ['2室1厅 ', ' 88.62平米 ', ' 北 南 ', ' 简装 ', ' 顶层(共6层) ', ' 2004年建 ', ' 板楼']
                    if len(L) >= 5:
                        item['model']=L[0].strip()
                        item['area']=L[1].strip()
                        item['direction']=L[2].strip()
                        item['perfect']=L[3].strip()
                        item['floor']=L[4].strip()
                #区域+总价+单价
                address_list=h.xpath('.//div[@class="positionInfo"]/a/text()')
                item['address']=address_list[0].strip() if address_list else None
                total_list=h.xpath('.//div[@class="totalPrice"]/span/text()')
                item['total_list']=total_list[0].strip() if total_list else  None
                price_list=h.xpath('.//div[@class="unitPrice"]/span/text()')
                item['price_list']=price_list[0].strip() if price_list else None
                print(item)
     # 入口函数
    def run(self):
        try:
            for i in range(1,101):
                url=self.url.format(i)
                self.parse_html(url)
                time.sleep(random.randint(1,3))
                #每次抓取一页要初始化一次self.blog
                self.blog=1
        except Exception as e:
            print('发生错误',e)
if __name__ == '__main__':
    spider=LinajiaSpider()
    spider.run()
目录
相关文章
|
数据采集 存储 数据可视化
【python】python二手房数据抓取分析可视化(源码)【独一无二】
【python】python二手房数据抓取分析可视化(源码)【独一无二】
【python】python二手房数据抓取分析可视化(源码)【独一无二】
|
API 数据处理 调度
DolphinScheduler教程(03)- 源码分析(二)
DolphinScheduler教程(03)- 源码分析(二)
667 0
|
人工智能 编解码 安全
【最新】火出圈的Sora到底是什么?5000字长文带你彻底了解Sora!
火出圈的Sora,是OpenAI最近发布的一款革命性文生视频大模型,引发了广泛的关注和讨论。在这篇5000字的长文中,我们将深入探讨Sora的技术特性、应用前景以及它对未来人工智能发展的潜在影响,带你彻底了解这个火出圈的Sora究竟是什么。
|
NoSQL Java 数据库
【SpringBoot】简述springboot项目启动数据加载内存中的三种方法
SpringBoot提供了两个接口: CommandLineRunner 和 ApplicationRunner 。实现其中接口,就可以在工程启动时将数据库中的数据加载到内存。使用的场景有:加载配置项到内存中;启动时将字典或白名单数据加载到内存(或缓存到Redis中)。
915 0
|
数据可视化 数据挖掘 大数据
Pandas+Pyecharts | 北京某平台二手房数据分析可视化
Pandas+Pyecharts | 北京某平台二手房数据分析可视化
|
25天前
|
数据可视化 搜索推荐 数据处理
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
本文介绍如何结合Dify Agent与AntV,打造高效、灵活的数据可视化解决方案。通过低代码构建数据流程,调用AntV丰富图表工具,实现从数据到多样可视化图表的快速生成,助力开发者兼顾效率与个性化需求。
224 0
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
|
机器学习/深度学习 数据采集 算法
重庆二手房数据爬取与分析实现
本文通过逻辑回归模型对重庆二手房市场数据进行爬取、分析和预测,研究了影响房价的关键变量特征,并提供了数据清洗、可视化和建模的详细步骤,旨在为投资者提供准确的市场趋势预测和投资建议。
252 2
重庆二手房数据爬取与分析实现
|
小程序 开发者
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到,一招解决
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到,一招解决
4730 0
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到,一招解决
|
JSON API 网络安全
App数据的爬取
App数据的爬取
385 3
|
7月前
|
人工智能 数据库管理 OLAP
Qwen3 + AnalyticDB+Dify on DMS 私有部署指导⽂档
Qwen3 + AnalyticDB+Dify on DMS 私有部署指导⽂档
1909 2