【问题标题】:Scrapy Spider not Following LinksScrapy Spider 不关注链接
【发布时间】:2015-06-18 17:44:47
【问题描述】:

我正在编写一个爬虫爬虫来从主页上抓取今天的 NYT 文章,但由于某种原因,它没有跟随任何链接。当我在scrapy shell http://www.nytimes.com 中实例化链接提取器时,它成功地提取了带有le.extract_links(response) 的文章网址列表,但是我无法让我的抓取命令(scrapy crawl nyt -o out.json)抓取除主页之外的任何内容。我有点不知所措了。是因为主页没有从解析函数中产生文章吗?任何帮助是极大的赞赏。

from datetime import date                                                       

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


from ..items import NewsArticle                                                 

with open('urls/debug/nyt.txt') as debug_urls:                                  
    debug_urls = debug_urls.readlines()                                         

with open('urls/release/nyt.txt') as release_urls:                              
    release_urls = release_urls.readlines() # ["http://www.nytimes.com"]                                 

today = date.today().strftime('%Y/%m/%d')                                       
print today                                                                     


class NytSpider(scrapy.Spider):                                                 
    name = "nyt"                                                                
    allowed_domains = ["nytimes.com"]                                           
    start_urls = release_urls                                                      
    rules = (                                                                      
            Rule(LinkExtractor(allow=(r'/%s/[a-z]+/.*\.html' % today, )),          
                 callback='parse', follow=True),                                   
    )                                                                              

    def parse(self, response):                                                     
        article = NewsArticle()                                                                         
        for story in response.xpath('//article[@id="story"]'):                     
            article['url'] = response.url                                          
            article['title'] = story.xpath(                                        
                    '//h1[@id="story-heading"]/text()').extract()                  
            article['author'] = story.xpath(                                       
                    '//span[@class="byline-author"]/@data-byline-name'             
            ).extract()                                                         
            article['published'] = story.xpath(                                 
                    '//time[@class="dateline"]/@datetime').extract()            
            article['content'] = story.xpath(                                   
                    '//div[@id="story-body"]/p//text()').extract()              
            yield article  

【问题讨论】:

    标签: python scrapy scrapy-spider


    【解决方案1】:

    我找到了解决问题的方法。我做错了两件事:

    1. 如果我希望它自动抓取子链接,我需要继承 CrawlSpider 而不是 Spider
    2. 使用CrawlSpider 时,我需要使用回调函数而不是覆盖parse。根据文档,覆盖 parse 会破坏 CrawlSpider 功能。

    【讨论】:

      猜你喜欢
      • 2015-02-01
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      相关资源
      最近更新 更多