zoukankan      html  css  js  c++  java
  • Scrapy 使用CrawlSpider整站抓取文章内容实现

    刚接触Scrapy框架,不是很熟悉,之前用webdriver+selenium实现过头条的抓取,但是感觉对于整站抓取,之前的这种用无GUI的浏览器方式,效率不够高,所以尝试用CrawlSpider来实现。
    这里通过一个实例加深对Scrapy框架的理解。

    本文开发环境:

    本文目标:抓取网站https://blog.scrapinghub.com的所有文章标题。

    1.首页的布局分析

    这个博客网站的页面结构如下图所示,我们的目标是抓取每页中间的item的标题,以及通过crawlspider自动抓取下一页的link链接达到自动抓取全站资源的目标。底部只有下一页的链接。
    在这里插入图片描述

    再来看第二页的布局:

    中间的内容跟首页一样,只是在底部的分页导航,多了上一页(首页只有下一页)
    在这里插入图片描述

    所以,文章标题所在布局我们搞清楚了,分页导航的格式也十分简单:https://blog.scrapinghub.com/page/2,
    只是最后一个数字不一样,这个数字就代表第几页文章。只是最后一个数字不一样,这个数字就代表第几页文章。

    2.VSCode实现基于Scrapy框架的CrawlSpider爬虫

    2.1 创建Scrapy工程

    终端进入到D盘的tmp目录输入如下命令创建一个名字为blogscrapy的scrapy工程(官方文档:):

    scrapy startproject blogscrapy

    在这里插入图片描述

    2.2 创建CrawlSpider爬虫

    终端进入到 D: mplogscrapy,输入如下命令创建一个名为blogspider,允许爬虫的域名为scrapinghub.com的基于crawlSpider模板的spider爬虫(官方文档:):

    scrapy genspider -t crawl blogspider blog.scrapinghub.com

    在这里插入图片描述

    2.3 定义item获取所需属性字段

    items.py的内容如下:

    # -*- coding: utf-8 -*-
    import scrapy
    class BlogscrapyItem(scrapy.Item):
        # define the fields for your item here like:
        # name = scrapy.Field()
        title = scrapy.Field()
    

    2.3 修改spider定义抓取规则、存储字段

    blog.py的内容如下:

    # -*- coding: utf-8 -*-
    import scrapy
    from scrapy.linkextractors import LinkExtractor
    from scrapy.spiders import CrawlSpider, Rule
    from blogscrapy.items import BlogscrapyItem
    
    class BlogSpider(CrawlSpider):
        name = 'blog'
        allowed_domains = ['blog.scrapinghub.com']
        start_urls = ['http://blog.scrapinghub.com/']
    
        rules = (
            Rule(LinkExtractor(restrict_xpaths=('//div[@class="blog-pagination"]/a', )),  callback='parse_item', follow=True),
        )
    
        def parse_item(self, response):
            for title in response.xpath('//div[@class="post-listing"]/div[@class="post-item"]/div[@class="post-header"]/h2'):
                item = BlogscrapyItem()
                item['title'] = title.xpath('./a/text()').extract_first()
                yield item
    

    pipeline.py的内容如下:

    # -*- coding: utf-8 -*-
    
    # Define your item pipelines here
    #
    # Don't forget to add your pipeline to the ITEM_PIPELINES setting
    # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
    
    import json
    
    class BlogscrapyPipeline(object):
        
        def __init__(self):
            self.file = open('pipeline-result.json', 'a+', encoding='utf-8')
    
        def process_item(self, item, spider):
            content = json.dumps(dict(item), ensure_ascii=False) + "
    "
            self.file.write(content)
            return item
    
        def close_spider(self, spider):
            self.file.close()
    
    

    最后,需要把pipeline的处理加到工程里面才能起作用,编辑工程目录下的settings.py文件,加入以下内容:

    ITEM_PIPELINES = {
        'blogscrapy.pipelines.BlogscrapyPipeline': 300,
    }
    
    

    3. 运行结果

    在这里插入图片描述

    上图看到成功爬取了12页内容,每页10条,总共115篇文章。

    4. 本文源码下载

    下载地址: 点我去下载


    参考资料

    [1]: Scrapy官方文档
    [2]: Python3爬取今日头条系列
    [3]: 廖雪峰老师的Python3 在线学习手册
    [4]: Python3官方文档
    [5]: 菜鸟学堂-Python3在线学习
    [6]: XPath语法参考

  • 相关阅读:
    软件测试常见概念
    Apollo简介及工作原理
    bug的编写技巧与级别划分
    native与H5优缺点及H5测试
    优惠券测试
    go语言-for循环
    go语言-流程控制--if
    go语言-二进制与位运算
    cookie和session
    AJAX
  • 原文地址:https://www.cnblogs.com/xiaocy66/p/10589276.html
Copyright © 2011-2022 走看看