实用scrapy批量下载自己的博客园文章
首先,在items.py中定义几个字段用来保存网页数据(网址,标题,网页源码)
如下所示:
import scrapy
class MycnblogsItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() page_title = scrapy.Field() page_url = scrapy.Field() page_html = scrapy.Field()
最重要的是我们的spider,我们这里的spider继承自CrawlSpider,方便我们定义正则来提示爬虫需要抓取哪些页面。
如:爬去下一页,爬去各个文章
在spdier中,我们使用parse_item方法来解析目标网页,从而得到文章的网址,标题和内容。
注:在parse_item方法中,我们在得到的html源码中,新增了base标签,这样打开下载后的html文件,不至于页面错乱,而是使用博客园的css样式
spdier源码如下:
# -*- coding: utf-8 -*- from mycnblogs.items import MycnblogsItem from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule class CnblogsSpider(CrawlSpider): name = "cnblogs" allowed_domains = ["cnblogs.com"] start_urls = ['http://www.cnblogs.com/hongfei/'] rules = ( # 爬取下一页,没有callback,意味着follow为True Rule(LinkExtractor(allow=('default.html\?page=\d+',))), # 爬取所有的文章,并使用parse_item方法进行解析,得到文章网址,文章标题,文章内容 Rule(LinkExtractor(allow=('hongfei/p/',)), callback='parse_item'), Rule(LinkExtractor(allow=('hongfei/articles/',)), callback='parse_item'), Rule(LinkExtractor(allow=('hongfei/archive/\d+/\d+/\d+/\d+.html',)), callback='parse_item'), ) def parse_item(self, response): item = MycnblogsItem() item['page_url'] = response.url item['page_title'] = response.xpath("//title/text()").extract_first() html = response.body.decode("utf-8") html = html.replace("<head>", "<head><base href='http://www.cnblogs.com/'>") item['page_html'] = html yield item
在pipelines.py文件中,我们使用process_item方法来处理返回的item
# -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html import codecs class MycnblogsPipeline(object): def process_item(self, item, spider): file_name = './blogs/' + item['page_title'] + '.html' with codecs.open(filename=file_name, mode='wb', encoding='utf-8') as f: f.write(item['page_html']) return item
以下是item pipeline的一些典型应用:
- 清理HTML数据
- 验证爬取的数据(检查item包含某些字段)
- 查重(并丢弃)
- 将爬取结果保存到数据库中
为了启用一个Item Pipeline组件,你必须将它的类添加到 ITEM_PIPELINES
配置,就像下面这个例子:
ITEM_PIPELINES = { 'mycnblogs.pipelines.MycnblogsPipeline': 300, }
程序运行后,将采集所有的文章到本地,如下所示:
原文地址:http://www.cnblogs.com/hongfei/p/6659934.html
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决