当前位置:网站首页>python_scrapy_房天下
python_scrapy_房天下
2020-11-08 08:04:00 【osc_x4ot1joy】
scrapy-讲解
xpath选取节点
常用的标签元素如下。
标记 | 描述 |
---|---|
extract | 提取内容转换为Unicode字符串,返回数据类型为list |
/ | 从根节点选取 |
// | 匹配选择的当前节点选择文档中的节点 |
. | 节点 |
@ | 属性 |
* | 任何元素节点 |
@* | 任何属性节点 |
node() | 任何类型的节点 |
爬取房天下-前奏
分析
1、网址:url:https://sh.newhouse.fang.com/house/s/。
2、确定爬取哪些数据:1)网页地址:page。2)所在位置名称:name。3)价格:price。4)地址:address。5)电话号码:tel
2、对网页进行分析。
打开url后,可以看到我们需要的数据,然后可以看下面还是有分页的。
可以看到打开url后查看网页元素,我们所要的数据都在一对ul标签内。
打开li一对标签,我们需要的name是在a标签下面的,而且在文本左右有不清楚的空格换行等需要特殊处理。
我们需要的price是在55000标签下面,注意,有的房子被买完了就没有价格显示,这个坑小心踩了。
一次类推我们可以找到对应的address和tel。
分页标签元素可以看到,当前页面的的a的class="active"。在打开主页面是a的文本是1,表示第一页。
爬取房天下-前具体实现过程
先新建scrapy项目
1)切换到项目文件夹:Terminal控制台上面输入 scrapy startproject hotel
,hotel是演示的项目名称,可以根据自己需要自定义。
2)根据需要在items.py文件夹下配置参数。在分析中可知需要用到五个参数,分别是:page,name,price,address,tel。配置代码如下:
class HotelItem(scrapy.Item):
# 这里的参数要与爬虫实现的具体参数一一对应
page = scrapy.Field()
name = scrapy.Field()
price = scrapy.Field()
address = scrapy.Field()
tel = scrapy.Field()
3)新建我们的爬虫分支。切换到spiders文件夹,Terminal控制台上面输入 scrapy genspider house sh.newhouse.fang.com
house是项目的爬虫名称,可以自定义,sh.newhouse.fang.com是爬取的区域选择。
在spider文件夹下面就有我们创建的house.py文件了。
代码实现与解释如下
import scrapy
from ..items import *
class HouseSpider(scrapy.Spider):
name = 'house'
# 爬取区域限制
allowed_domains = ['sh.newhouse.fang.com']
# 爬取的主页面
start_urls = ['https://sh.newhouse.fang.com/house/s/',]
def start_requests(self):
for url in self.start_urls:
# 回掉函数传的模块名称,没有括号。这是一种约定。
yield scrapy.Request(url=url,callback=self.parse)
def parse(self, response):
items = []
# 获取当前页面显示的值
for p in response.xpath('//a[@class="active"]/text()'):
# extract使提取内容转换为Unicode字符串,返回数据类型为list
currentpage=p.extract()
# 确定最后一页
for last in response.xpath('//a[@class="last"]/text()'):
lastpage=last.extract()
# 切换到最近一层的标签。//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置 /从根节点选取
for each in response.xpath('//div[@class="nl_con clearfix"]/ul/li/div[@class="clearfix"]/div[@class="nlc_details"]'):
item=HotelItem()
# 名称
name=each.xpath('//div[@class="house_value clearfix"]/div[@class="nlcd_name"]/a/text()').extract()
# 价格
price=each.xpath('//div[@class="nhouse_price"]/span/text()').extract()
# 地址
address=each.xpath('//div[@class="relative_message clearfix"]/div[@class="address"]/a/@title').extract()
# 电话
tel=each.xpath('//div[@class="relative_message clearfix"]/div[@class="tel"]/p/text()').extract()
# 所有item里面参数要与我们items里面参数意义对应
item['name'] = [n.replace(' ', '').replace("\n", "").replace("\t", "").replace("\r", "") for n in name]
item['price'] = [p for p in price]
item['address'] = [a for a in address]
item['tel'] = [s for s in tel]
item['page'] = ['https://sh.newhouse.fang.com/house/s/b9'+(str)(eval(p.extract())+1)+'/?ctm=1.sh.xf_search.page.2']
items.append(item)
print(item)
# 当爬取到最后一页,类标签last就自动切换成首页
if lastpage=='首页':
pass
else:
# 如果不是最后一页,继续爬取下一页数据,知道爬完所有数据
yield scrapy.Request(url='https://sh.newhouse.fang.com/house/s/b9'+(str)(eval(currentpage)+1)+'/?ctm=1.sh.xf_search.page.2', callback=self.parse)
4)在spiders下运行爬虫,Terminal控制台上面输入 scrapy crawl house
。
结果如下图所示
整体项目结构如右图
tts文件夹是我这边用于存储数据的的txt文件。本项目里面可以不需要。
如有发现错误请联系微信:sunyong8860
python的路上爬着前行
版权声明
本文为[osc_x4ot1joy]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4383219/blog/4707854
边栏推荐
- 什么你的电脑太渣?这几招包你搞定! (Win10优化教程)
- 你的主机中的软件中止了一个已建立的连接。解决方法
- 归纳一些比较好用的函数
- LadonGo开源全平台渗透扫描器框架
- What details does C + + improve on the basis of C
- ts流中的pcr与pts计算与逆运算
- C语言I博客作业03
- Adobe Prelude /Pl 2020软件安装包(附安装教程)
- Astra: the future of Apache Cassandra is cloud native
- About the promotion of the whole stack of engineers, from the introduction to give up the secret arts, do not click in to have a look?
猜你喜欢
C/C++编程笔记:C语言相比其他编程语言,有什么不一样的优势?
Qt混合Python开发技术:Python介绍、混合过程和Demo
Qt混合Python开发技术:Python介绍、混合过程和Demo
Get tree menu list
Sum up some useful functions
Ladongo open source full platform penetration scanner framework
Judging whether paths intersect or not by leetcode
Insight -- the application of sanet in arbitrary style transfer
Mouse small hand
These core technology of object-oriented, after you master it, you can have a good interview
随机推荐
Judging whether paths intersect or not by leetcode
哔哩哔哩常用api
PCR and PTS calculation and inverse operation in TS stream
Summary of knowledge points of Jingtao project
QT hybrid Python development technology: Python introduction, hybrid process and demo
C语言I博客作业03
More than 50 object detection datasets from different industries
Oschina plays on Sunday - before that, I always thought I was a
GoLand writes a program with template
2020天翼智能生态博览会中国电信宣布5G SA正式规模商用
About the promotion of the whole stack of engineers, from the introduction to give up the secret arts, do not click in to have a look?
leetcode之判断路径是否相交
Codeforce算法题 | 你能想出解法,让你的基友少氪金吗?
use Xunit.DependencyInjection Transformation test project
scala 中 Future 的简单使用
Lay UI left tree Dtree right list table
SQL Server 2008R2 18456 error resolution
Everything is 2020, LINQ query you are still using expression tree
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Tail delivery