Python Scrapy多页数据爬取实现过程解析
作者:Hedger_Lee 时间:2021-02-28 08:14:01
1.先指定通用模板
url = 'https://www.qiushibaike.com/text/page/%d/'#通用的url模板
pageNum = 1
2.对parse方法递归处理
parse第一次调用表示的是用来解析第一页对应页面中的数据
对后面的页码的数据要进行手动发送
if self.pageNum <= 5:
self.pageNum += 1
new_url = format(self.url%self.pageNum)
#手动请求(get)的发送
yield scrapy.Request(new_url,callback=self.parse)
完整示例
class QiubaiSpider(scrapy.Spider):
name = 'qiubai'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://www.qiushibaike.com/text/']
url = 'https://www.qiushibaike.com/text/page/%d/'#通用的url模板
pageNum = 1
#parse第一次调用表示的是用来解析第一页对应页面中的段子内容和作者
def parse(self, response):
div_list = response.xpath('//*[@id="content-left"]/div')
all_data = []
for div in div_list:
author = div.xpath('./div[1]/a[2]/h2/text()').extract_first()
content = div.xpath('./a[1]/div/span//text()').extract()
content = ''.join(content)
# 将解析的数据存储到item对象
item = QiubaiproItem()
item['author'] = author
item['content'] = content
# 将item提交给管道
yield item # item一定是提交给了优先级最高的管道类
if self.pageNum <= 5:
self.pageNum += 1
new_url = format(self.url%self.pageNum)
#手动请求(get)的发送
yield scrapy.Request(new_url,callback=self.parse)
来源:https://www.cnblogs.com/Hedger-Lee/p/13072506.html
标签:Python,Scrapy,爬取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python编程入门的一些基本知识
2023-09-07 00:07:24
![](https://img.aspxhome.com/file/2023/9/112019_0s.jpg)
详谈python中subprocess shell=False与shell=True的区别
2021-08-01 02:00:01
![](https://img.aspxhome.com/file/2023/9/84899_0s.jpg)
sqlserver通用的删除服务器上的所有相同后缀的临时表
2012-06-06 20:07:34
python3使用pyqt5制作一个超简单浏览器的实例
2023-04-12 19:44:51
Python实现Tab自动补全和历史命令管理的方法
2022-06-16 10:12:46
Python 函数list&read&seek详解
2022-02-14 03:04:21
![](https://img.aspxhome.com/file/2023/7/86447_0s.png)
python dataframe向下向上填充,fillna和ffill的方法
2021-11-07 18:16:47
5个Python杀手级的自动化脚本分享
2022-11-21 12:03:54
python 利用pandas将arff文件转csv文件的方法
2021-05-31 05:49:34
使用Python编写爬虫的基本模块及框架使用指南
2021-08-21 17:45:19
python 实现任务管理清单案例
2023-09-01 04:59:17
![](https://img.aspxhome.com/file/2023/7/118297_0s.jpg)
Python 机器学习库 NumPy入门教程
2022-09-28 23:10:41
一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
2023-01-09 19:50:16
![](https://img.aspxhome.com/file/2023/3/79623_0s.jpg)
关于python中remove的一些坑小结
2022-09-22 02:14:31
![](https://img.aspxhome.com/file/2023/6/118626_0s.png)
纯CSS制作的网页中的lightbox效果
2007-11-06 18:59:00
Django实现文件上传下载
2022-03-17 18:42:48
简单介绍Python中的readline()方法的使用
2023-11-02 13:34:30
Python爬取当网书籍数据并数据可视化展示
2023-11-20 11:31:14
![](https://img.aspxhome.com/file/2023/7/76907_0s.png)
web标准:CSS clear的属性及使用方法
2008-10-27 13:31:00
C++/Php/Python/Shell 程序按行读取文件或者控制台的实现
2021-12-20 06:36:18