当前位置:网站首页>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
边栏推荐
- iOS 学习笔记二【cocopods安装使用和安装过程中遇到的问题及解决办法】【20160725更新】
- November 07, 2020: given an array of positive integers, the sum of two numbers equals N and must exist. How to find the two numbers with the smallest multiplication?
- CPP (4) boost installation and basic use for Mac
- 搜索引擎的日常挑战_4_外部异构资源 - 知乎
- 麦格理银行借助DataStax Enterprise (DSE) 驱动数字化转型
- Android Basics - RadioButton (radio button)
- Solve the problem of rabbitmq message loss and repeated consumption
- What? Your computer is too bad? You can handle these moves! (win10 optimization tutorial)
- 面部识别:攻击类型和反欺骗技术
- Cryptography - Shangsi Valley
猜你喜欢
来自不同行业领域的50多个对象检测数据集
Astra: Apache Cassandra的未来是云原生
【原创】关于高版本poi autoSizeColumn方法异常的情况
OSChina 周日乱弹 —— 之前呢,我一直以为自己是个……
模板链表类学习
Hand tearing algorithm - handwritten singleton mode
UCGUI简介
[solution] distributed timing task solution
swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
Do you really understand the high concurrency?
随机推荐
C expression tree (1)
C / C + + Programming Notes: what are the advantages of C compared with other programming languages?
Golang anonymous structure member, named structure member, inheritance, composition
什么你的电脑太渣?这几招包你搞定! (Win10优化教程)
ts流中的pcr与pts计算与逆运算
在Ubuntu上体验最新版本EROFS
Is blazor ready to serve the enterprise?
Search and replace of sed
Python3.9的7个特性
Do you really understand the high concurrency?
微信昵称emoji表情,特殊表情导致列表不显示,导出EXCEL报错等问题解决!
swiper 窗口宽度变化,页面宽度高度变化 导致自动滑动 解决方案
C language I blog assignment 03
VC6 compatibility and open file crash resolution
洞察——风格注意力网络(SANet)在任意风格迁移中的应用
Abnormal + Abstract
哔哩哔哩常用api
nvm
More than 50 object detection datasets from different industries
ROS learning: remote start ROS node