链家二手房爬取保存到excel中

本文介绍如何使用Python爬取链家二手房数据,解析HTML,存储为CSV文件,并进一步将数据导出到Excel,实现数据分析的初步准备。
摘要由CSDN通过智能技术生成

​今天呢,我来爬取一下链家,拿到一些数据,方便后面进行数据分析。
首先,打开主页:
在这里插入图片描述
观察网页信息,发现不是Ajax请求,就是一个简单的获取就行了。
我们开始办事情:
在这里插入图片描述
可以看到,这段信息在源码中是这样的。
在这里插入图片描述
请求状态码是200,证明请求成功了。
我们现在来获取数据
在这里插入图片描述
已经把数据爬取下来了。
我们要把图上的数据都爬取下来,保存到本地
在这里插入图片描述
代码走:

import requests
from lxml import etree
url = "https://bj.lianjia.com/ershoufang/pg{}rs北京/"
for i in range(1,2):
    print(i)
    url_str=url.format(i)
    print(url_str)
    headers={
   "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"}
    res=requests.get(url_str,headers=headers)
# print(res.content.decode())
    html=res.content.decode()
    html_1=etree.HTML(html)
    houseInfo=html_1.xpath('//div[@class="houseInfo"]/text()')
    title=html_1.xpath('//div[@class="title"]/a/text()')
    positionInfo=html_1.xpath('//div[@class="positionInfo"]/a/text()')
    totalPrice=html_1.xpath('//div[@class="totalPrice"]/span/text()')
    unitPrice=html_1.xpath('//div[@class="unitPrice"]/span/text()')
    followInfo=html_1.xpath('//div[@class="followInfo"]/text()')
    tag=html_1.xpath('//div[@class="tag"]/span/text()')
    print("****************")
    print(title)
    print("****************")
    print(positionInfo)
    print("****************")
    print(houseInfo)
    print("****************"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值