【问题标题】:How to store the URLs crawled with Scrapy?Scrapy爬取的网址如何存储?
【发布时间】:2013-02-27 07:00:55
【问题描述】:

我有一个网络爬虫,可以爬取网页上的新闻报道。

我知道如何使用 XpathSelector 从页面中的元素中抓取某些信息。

但我似乎无法弄清楚如何存储刚刚抓取的页面的 URL。

class spidey(CrawlSpider):
    name = 'spidey'
    start_urls = ['http://nytimes.com'] # urls from which the spider will start crawling
    rules = [Rule(SgmlLinkExtractor(allow=[r'page/\d+']), follow=True), 
        # r'page/\d+' : regular expression for http://nytimes.com/page/X URLs
        Rule(SgmlLinkExtractor(allow=[r'\d{4}/\d{2}/\w+']), callback='parse_articles')]
        # r'\d{4}/\d{2}/\w+' : regular expression for http://nytimes.com/YYYY/MM/title URLs

我想存储通过这些规则的每个链接。

我需要添加什么到 parse_articles 以将链接存储在我的项目中?

def parse_articles(self, response):
    item = SpideyItem()
    item['link'] = ???
    return item

【问题讨论】:

    标签: python scrapy web-crawler


    【解决方案1】:

    response.url 是您要查找的内容。

    查看响应对象上的docs 并查看this 简单示例。

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2018-05-05
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多