【问题标题】:How to follow next pages in Scrapy Crawler to scrape content如何在 Scrapy Crawler 中跟随下一页来抓取内容
【发布时间】:2016-05-20 10:42:00
【问题描述】:

我能够从第一页抓取所有故事,我的问题是如何移动到下一页并继续抓取故事和名称,请检查下面的代码

# -*- coding: utf-8 -*-
import scrapy
from cancerstories.items import CancerstoriesItem
class MyItem(scrapy.Item):
    name = scrapy.Field()
    story = scrapy.Field()
class MySpider(scrapy.Spider):

    name = 'cancerstories'
    allowed_domains = ['thebreastcancersite.greatergood.com']
    start_urls = ['http://thebreastcancersite.greatergood.com/clickToGive/bcs/stories/']

    def parse(self, response):

        rows = response.xpath('//a[contains(@href,"story")]')

        #loop over all links to stories
        for row in rows:
            myItem = MyItem() # Create a new item
            myItem['name'] = row.xpath('./text()').extract() # assign name from link
            story_url = response.urljoin(row.xpath('./@href').extract()[0]) # extract url from link
            request = scrapy.Request(url = story_url, callback = self.parse_detail) # create request for detail page with story
            request.meta['myItem'] = myItem # pass the item with the request
            yield request

    def parse_detail(self, response):
        myItem = response.meta['myItem'] # extract the item (with the name) from the response
        #myItem['name']=response.xpath('//h1[@class="headline"]/text()').extract()
        text_raw = response.xpath('//div[@class="photoStoryBox"]/div/p/text()').extract() # extract the story (text)
        myItem['story'] = ' '.join(map(unicode.strip, text_raw)) # clean up the text and assign to item
        yield myItem # return the item

【问题讨论】:

    标签: python-2.7 scrapy web-crawler


    【解决方案1】:

    如果您使用 scrapy.spider 类,您可以手动关注下一页,例如: next_page = response.css('a.pageLink ::attr(href)').extract_first() 如果是下一页: absolute_next_page_url = response.urljoin(next_page) 产生 scrapy.Request(url=absolute_next_page_url, callback=self.parse) 如果要使用 CralwSpider 类,请不要忘记将 parse 方法重命名为 parse_start_url

    【讨论】:

      【解决方案2】:

      您可以将您的scrapy.Spider 更改为CrawlSpider,并使用RuleLinkExtractor 跟随链接到下一页。

      对于这种方法,您必须包含以下代码:

      ...
      from scrapy.spiders import CrawlSpider, Rule
      from scrapy.linkextractors import LinkExtractor
      ...
      rules = (
              Rule(LinkExtractor(allow='\.\./stories;jsessionid=[0-9A-Z]+?page=[0-9]+')),
      )
      ...
      class MySpider(CrawlSpider):
      ...
      

      这样,对于您访问的每个页面,蜘蛛都会创建一个对下一页(如果存在)的请求,在完成 parse 方法的执行后跟随它,并再次重复该过程。

      编辑:

      我写的规则只是跟随下一页链接而不是提取故事,如果您的第一种方法有效,则无需更改它。

      另外,关于您评论中的规则,SgmlLinkExtractor 已被弃用,因此我建议您使用默认的link extractor,并且该规则本身没有明确定义。

      当提取器中的参数attrs 未定义时,它会搜索链接以查找正文中的href 标签,在这种情况下看起来像../story/mother-of-4435 而不是/clickToGive/bcs/story/mother-of-4435。这就是它找不到任何链接的原因。

      【讨论】:

      • Rule(SgmlLinkExtractor(allow=('/clickToGive\/bcs\/stories\?\page\=[0-9]+'), ), callback="parseme", follow= True ),添加这个甚至不会抓取第一页
      • 我编辑答案以对评论进行适当的回复,希望对您有所帮助
      • 是的,谢谢,linkextractor 现在可以跟随链接,但它似乎不断地将页面刮到最后,然后再次跟随上一个链接到开头
      • 这种行为响应了蜘蛛底层的 Scrapy 架构。您正在尝试按照蜘蛛中所写的方式执行,但数据流有点复杂。我建议你阅读这个页面,它有助于理解这个过程:doc.scrapy.org/en/latest/topics/architecture.html
      • 我设法正确获取了所有内容,非常感谢您的帮助@javitronxo
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2023-03-30
      • 2021-06-21
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      相关资源
      最近更新 更多