当前位置:网站首页>python_scrapy_房天下
python_scrapy_房天下
2020-11-08 08:04:32 【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
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World