爬取二手房案例
爬取二手房案例
@TOC前言本文通过一个爬取二手房的案例,来分享另外一种解析数据的方式:解析神器python第三方库parsel库。之所以叫他解析神奇,是因为它支持三种解析方式。可以通过Xpath,CSS选择器和正则表达式来提取HTML或XML文档中的数据。导航爬取小说案例-BeautifulSoup教学篇爬取二手房案例--parsel教学篇(CSS选择器)爬取美国公司案例-parsel库教学篇(
爬取二手房案例
@TOC
前言
本文通过一个爬取二手房的案例,来分享另外一种解析数据的方式:解析神器python第三方库parsel库。之所以叫他解析神奇,是因为它支持三种解析方式。可以通过Xpath,CSS选择器和正则表达式来提取HTML或XML文档中的数据。
导航
- 爬取小说案例-BeautifulSoup教学篇
- 爬取二手房案例--parsel教学篇(CSS选择器)
- 爬取美国公司案例-parsel库教学篇(Xpath的详细使用)
- 爬取东方财富网-parsel教学篇(正则表达式的详细使用+实例)
- 爬取QQ音乐的评论-JSO库的详细使用
安装parsel
因为它是第三方库,所以需要在终端使用pip install parsel 来安装
代码语言:bash复制pip install parsel
创建Selector对象
代码语言:python代码运行次数:0运行复制url="xxx"
resp=requests.get(url)
selector=
解析数据
解析数据有CSS选择器,Xpath和正则表达式,下面通过一个例子来分别介绍这三种解析方式
代码语言:html复制<html>
<head>
<title>Example</title>
</head>
<body>
<div class="wrap">
<div id="container">
<ul class="list">
<li class="item-0">first item</li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-0 active"><a href="link.html"><span id="bold">third item</span>
<span id="test">test</span>
</a></li>
<li class="item-1 active"><a href="link4.html">fourth item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a></li>
</ul>
</div>
</div>
</html>
CSS选择器
代码语言:python代码运行次数:0运行复制# get()和get\_all()区别
## get():用于从通过选择器定位到的元素中提取第一个匹配项的文本内容或属性值,返回的是字符串。
## get\_all():用于通过选择器定位到的元素中提取所有匹配项的文本内容或属性值,返回的是列表
# 标签选择器
res = (tagame)
# 例如:提取所有li标签中的文字
li\_data = ("li::text").getall()
# class选择器
res = ()
# 例如 提取class为item-1的li标签的内容
li\_data = ("li.item-1::text").get()
# id选择器
res = (tagame#idame)
# 例如:提取id为container的div标签的内容
li\_data = ("div#container::text").get()
# 属性提取器
res = (tagame::attr(attrame))
# 例如:提取class为item-1的li标签中的href属性
res = ("li.item-1::attr(href)").get()
# 后代选择器(如div p)
# 例如:选择id为container的div标签下的所有span标签的内容
res = ("div#container span:text").get()
# 子选择器(如div > p)
# 例如:选择id为container的div标签下的所有span标签的内容(和上面不同的是这个标签必须在div的直接子代)
res = ("div#container>span:text").get()
# 嵌套选择器
# 例如:提取 class为item-0 li标签内的id为bold的span标签的内容
res = ("li.item-0 span#bold:text").get()
# 伪类选择器
# 例如:选择父级元素ul下的第二个li标签直接子代的内容
res = ('ul>li:nth-child(2)::text').get()
Xpath
敬请下篇
正则表达式
敬请下篇
下面通过一个爬取二手房安居客的实例来更深入的了解css选择器的用法吧
爬取安居客二手房实例
代码语言:python代码运行次数:0运行复制import requests # 数据请求模块
import parsel # 数据解析库
import csv # 存储到表格中
import os # 文件管理模块
with open("", 'wb') as f:
f = open('', mode='a', encoding='utf-8', newline='')
csv\_writer = csv.DictWriter(f, fieldnames=["小区名", "小区区域", "小区户型", "小区面积", "均价"
])
csv\_writer.writeheader()
# 目标网站:安居客二手房网站
url="/"
headers={
"User-Agent": "Mozilla/5.0 (Windows T 10.0; Win64; x64) AppleWebKit/57.6 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/57.6 Edg/126.0.0.0",
"Cookie":"xxx"
}
# Cookie可以通过F12键 查看网络数据包,请求标头中。 如下面
respe = requests.get(url=url, headers=headers)
# 实例化对象
selector = parsel.Selector()
# 爬取十页
for num in range(1,11):
url=f'://wuhan.anjuke/sale/hongshana/p{num}/'
print(f"正在抓取:第{num}页")
# class选择器
res\_all=('div .property')
for res in res\_all:
community=('p.property-content-info-comm-name::text').get()
community\_address=("p.property-content-info-comm-address span::text").getall()
community\_address="".join(community\_address)
community\_house=('div.property-content-info p.property-content-info-text span::text').getall()
community\_house="".join(community\_house)
community\_area = ('div.property-content-info p:nth-child(2)::text').get().strip()
community\_area = "".join(community\_area)
community\_average=('p.property-price-average::text').get().strip()
print("小区名:",community,"小区区域:",community\_address,"小区户型:",community\_house,"小区面积:",community\_area,"均价",community\_average)
dic = {
'小区名': community,
'小区区域': community\_address,
'小区户型':community\_house,
'小区面积': community\_area,
'均价': community\_average
}
# 写入表格
csv\_writer.writerow(dic)
这里Cookie
运行截图
共勉
财富是对认知的补偿,不是对勤奋的奖赏。
博客
- 本人是一个渗透爱好者,不时会在(laity的渗透测试之路)更新一些实战渗透的实战案例,感兴趣的同学可以关注一下,大家一起进步。
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-21 18:34:59
推荐阅读
留言与评论(共有 6 条评论) |
本站网友 fuelcell | 19分钟前 发表 |
url=f' | |
本站网友 神州三号 | 8分钟前 发表 |
nth-child(2) | |
本站网友 夜色茫茫 | 15分钟前 发表 |
f = open('' | |
本站网友 家具与家俱 | 8分钟前 发表 |
community | |
本站网友 北京海润国际公寓 | 27分钟前 发表 |
是因为它支持三种解析方式 |