Python爬虫从入门到精通:(36)CrawlSpider实现深度爬取_Python涛哥

2024-03-17 01:32

本文主要是介绍Python爬虫从入门到精通:(36)CrawlSpider实现深度爬取_Python涛哥,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们来看下CrawlSpider实现深度爬取。

爬取阳光热线标题、状态、和详情页内容。

https://wz.sun0769.com/political/index/politicsNewest?id=1&type=4&page=


创建CrawlSpider工程

  1. scrapy startproject sunPro

  2. cd sunPro

  3. scrapy genspider -t crawl sun www.xxx.com

  4. 修改配置文件等

在这里插入图片描述


页面解析

提取下页码链接

我们看到这个网站有很多页面,我们先来提取下页码链接。
在这里插入图片描述

很容易分析到页面链接的规律,写下正则:

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Ruleclass SunSpider(CrawlSpider):name = 'sun'# allowed_domains = ['www.xxx.com']start_urls = ['https://wz.sun0769.com/political/index/politicsNewest?id=1&type=4&page=']# 提取页码链接link = LinkExtractor(allow=r'id=1&page=\d+')rules = (Rule(link, callback='parse_item', follow=True),)def parse_item(self, response):print(response)

在这里插入图片描述

这里我们主要学习深度爬取,后面只用一页作为案例。follow=False

数据解析

我们来获取当前页的标题、详情页地址和状态

在这里插入图片描述

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunPro.items import SunproItemclass SunSpider(CrawlSpider):name = 'sun'# allowed_domains = ['www.xxx.com']start_urls = ['https://wz.sun0769.com/political/index/politicsNewest?id=1&type=4&page=']# 提取页码链接link = LinkExtractor(allow=r'id=1&page=\d+')rules = (Rule(link, callback='parse_item', follow=False),)# 页面数据解析def parse_item(self, response):li_list = response.xpath('/html/body/div[2]/div[3]/ul[2]/li')for li in li_list:title = li.xpath('./span[3]/a/text()').extract_first()detail_url = 'https://wz.sun0769.com' + li.xpath('./span[3]/a/@href').extract_first()status = li.xpath('./span[2]/text()').extract_first()# 保存item提交给管道item = SunproItem()item['title'] = titleitem['detail_url'] = detail_urlitem['status'] = status**手动发送请求**现在我们用手动发送请求的方式解析详情页数据:```python
# 页面数据解析
def parse_item(self, response):li_list = response.xpath('/html/body/div[2]/div[3]/ul[2]/li')for li in li_list:title = li.xpath('./span[3]/a/text()').extract_first()detail_url = 'https://wz.sun0769.com' + li.xpath('./span[3]/a/@href').extract_first()status = li.xpath('./span[2]/text()').extract_first()# 保存item提交给管道item = SunproItem()item['title'] = titleitem['detail_url'] = detail_urlitem['status'] = statusyield scrapy.Request(url=detail_url, callback=self.parse_detail, meta={'item': item})# 详情页数据解析
def parse_detail(self, response):content = response.xpath('/html/body/div[3]/div[2]/div[2]/div[2]/pre/text()').extract_first()item = response.meta['item']item['content'] = contentyield item

运行一下,我们就获取了全部数据

在这里插入图片描述


完整代码:

sum.py

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunPro.items import SunproItemclass SunSpider(CrawlSpider):name = 'sun'# allowed_domains = ['www.xxx.com']start_urls = ['https://wz.sun0769.com/political/index/politicsNewest?id=1&type=4&page=']# 提取页码链接link = LinkExtractor(allow=r'id=1&page=\d+')rules = (Rule(link, callback='parse_item', follow=False),)# 页面数据解析def parse_item(self, response):li_list = response.xpath('/html/body/div[2]/div[3]/ul[2]/li')for li in li_list:title = li.xpath('./span[3]/a/text()').extract_first()detail_url = 'https://wz.sun0769.com' + li.xpath('./span[3]/a/@href').extract_first()status = li.xpath('./span[2]/text()').extract_first()# 保存item提交给管道item = SunproItem()item['title'] = titleitem['status'] = statusyield scrapy.Request(url=detail_url, callback=self.parse_detail, meta={'item': item})# 详情页数据解析def parse_detail(self, response):content = response.xpath('/html/body/div[3]/div[2]/div[2]/div[2]/pre/text()').extract_first()item = response.meta['item']item['content'] = contentyield item

items.py

import scrapyclass SunproItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()title = scrapy.Field()status = scrapy.Field()content = scrapy.Field()

Pipeline.py

class SunproPipeline:def process_item(self, item, spider):print(item)return item

settings.py

略~请自己学会熟练配置!


总结

CrawlSpider实现的深度爬取

  • 通用方式:CrawlSpider + Spider实现

关注Python涛哥!学习更多Python知识!

这篇关于Python爬虫从入门到精通:(36)CrawlSpider实现深度爬取_Python涛哥的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817382

相关文章

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B