当前位置:网站首页>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.comhouse是项目的爬虫名称,可以自定义,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
边栏推荐
- Using subprocess residue in supervisor and python multiprocessing
- The software in your host has terminated an established connection. resolvent
- 2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
- Littlest jupyterhub| 02 using nbgitpuller to distribute shared files
- 你的主机中的软件中止了一个已建立的连接。解决方法
- Daily challenges of search engines_ 4_ External heterogeneous resources - Zhihu
- 5g + Ar out of the circle, China Mobile Migu becomes the whole process strategic partner of the 33rd China Film Golden Rooster Award
- SQL Server 2008R2 18456 error resolution
- QT hybrid Python development technology: Python introduction, hybrid process and demo
- 16.文件传输协议、vsftpd服务
猜你喜欢

IOS upload app store error: this action cannot be completed - 22421 solution

微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!

Adobe Prelude /Pl 2020软件安装包(附安装教程)

Idea - the. IML file was not automatically generated by the project

1.深入Istio:Sidecar自动注入如何实现的?

鼠标变小手

scala 中 Future 的简单使用

Qt混合Python开发技术:Python介绍、混合过程和Demo

C++在C的基础上改进了哪些细节

C language I blog assignment 03
随机推荐
PerconaXtraDBCluster8.0 最详尽用法指南
iOS上传App Store报错:this action cannot be completed -22421 解决方案
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?
Windows subsystem Ubuntu installation
Face recognition: attack types and anti spoofing techniques
Mouse small hand
16. File transfer protocol, vsftpd service
Unparseable date: 'Mon Aug 15 11:24:39 CST 2016',时间格式转换异常
京淘项目知识点总结
SQL Server 2008R2 18456 error resolution
Sentry installation
5g + Ar out of the circle, China Mobile Migu becomes the whole process strategic partner of the 33rd China Film Golden Rooster Award
OSChina 周日乱弹 —— 之前呢,我一直以为自己是个……
Idea - the. IML file was not automatically generated by the project
C语言I博客作业03
Insight -- the application of sanet in arbitrary style transfer
IOS upload app store error: this action cannot be completed - 22421 solution
Web Security (4) -- XSS attack
Abnormal + Abstract
Privacy violation and null dereference of fortify vulnerability