【问题标题】:Alternative to scrapy.contrib in scrapy spider code在scrapy spider代码中替代scrapy.contrib
【发布时间】:2022-01-16 22:51:24
【问题描述】:

我在一本关于网络抓取的教科书中找到了这个示例代码。运行蜘蛛后它显示错误并发现 scrapy.contrib 在 1.16 版本的 scrapy 中被删除。我应该如何改变它才能工作。顺便说一句,我是网络抓取的新手。

from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule

class ArticleSpider(CrawlSpider):
    name = 'articles'
    allowed_domains = ['wikipedia.org']
    start_urls = ['https://en.wikipedia.org/wiki/'
                  'Benevolent_dictator_for_life']
    rules = [Rule(LinkExtractor(allow='.*'), callback='parse_items',
                  follow=True)]

    def parse_items(self, response):
        url = response.url
        title = response.css('h1::text').extract_first()
        text = response.xpath('//div[@id="mw-content-text"]//text()').extract()
        lastUpdated = response.css('li#footer-info-lastmod::text').extract_first()
        lastUpdated = lastUpdate.replace(
            'This page was last edited on ','')
        print('URL is: {}'.format(url))
        print('title is: {}'.format(title))
        print('text is: {}'.format(text))
        print('Last updated: {}'.format(lastUpdated))

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    在较新版本的scrapy中,您可以简单地导入以下模块

    from scrapy.spiders import CrawlSpider, Rule
    from scrapy.linkextractors import LinkExtractor
    # add the rest of the code
    

    阅读更多来自docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      相关资源
      最近更新 更多