您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:python爬取链家二手房的数据

51自学网 2021-10-30 22:39:23
  python
这篇教程python爬取链家二手房的数据写得很实用,希望能帮到您。

一、查找数据所在位置:

打开链家官网,进入二手房页面,选取某个城市,可以看到该城市房源总数以及房源列表数据。

二、确定数据存放位置:

某些网站的数据是存放在html中,而有些却api接口,甚至有些加密在js中,还好链家的房源数据是存放到html中:

三、获取html数据:

通过requests请求页面,获取每页的html数据

# 爬取的url,默认爬取的南京的链家房产信息url = 'https://nj.lianjia.com/ershoufang/pg{}/'.format(page)# 请求urlresp = requests.get(url, headers=headers, timeout=10)

四、解析html,提取有用数据:

通过BeautifulSoup解析html,并提取相应有用的数据

soup = BeautifulSoup(resp.content, 'lxml')# 筛选全部的li标签sellListContent = soup.select('.sellListContent li.LOGCLICKDATA')# 循环遍历for sell in sellListContent:    # 标题    title = sell.select('div.title a')[0].string    # 先抓取全部的div信息,再针对每一条进行提取    houseInfo = list(sell.select('div.houseInfo')[0].stripped_strings)    # 楼盘名字    loupan = houseInfo[0]    # 对楼盘的信息进行分割    info = houseInfo[0].split('|')    # 房子类型    house_type = info[1].strip()    # 面积大小    area = info[2].strip()    # 房间朝向    toward = info[3].strip()    # 装修类型    renovation = info[4].strip()    # 房屋地址    positionInfo = ''.join(list(sell.select('div.positionInfo')[0].stripped_strings))    # 房屋总价    totalPrice = ''.join(list(sell.select('div.totalPrice')[0].stripped_strings))    # 房屋单价    unitPrice = list(sell.select('div.unitPrice')[0].stripped_strings)[0]

以上就是我的分享,如果有什么不足之处请指出,多交流,谢谢!

以上就是python爬取链家二手房的数据的详细内容,更多关于python爬取链家二手房的资料请关注51zixue.net其它相关文章!


用python实现监控视频人数统计
python 爬取英雄联盟皮肤图片
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。