【问题标题】:How to include the start url in the "allow" rule in SgmlLinkExtractor using a scrapy crawl spider如何使用爬虫爬虫在 SgmlLinkExtractor 的“允许”规则中包含起始 URL
【发布时间】:2012-01-07 18:28:31
【问题描述】:

我搜索了很多主题,但似乎没有找到我特定问题的答案。 我为一个网站创建了一个爬虫,它运行良好。然后我做了一个类似的爬取类似的网站,但这次我有一个小问题。言归正传:

我的起始网址如下所示: www.example.com 。该页面包含我想应用我的蜘蛛的链接,如下所示:

  • www.example.com/locationA
  • www.example.com/locationB
  • www.example.com/locationC

...

我现在有一个问题: 每次我输入起始网址时,它都会自动重定向到 www.example.com/locationA 并且我让我的蜘蛛工作的所有链接都包括

  • www.example.com/locationB
  • www.example.com/locationC ...

所以我的问题是如何在返回的 URL 中包含 www.example.com/locationA。我什至得到如下日志信息:

-2011-11-28 21:25:33+1300 [example.com] 调试:重定向 (302) 到来自 http://www.example.com/>

-2011-11-28 21:25:34+1300[example.com] 调试:将 (302) 重定向到(引用者:无)

  • 2011-11-28 21:25:37+1300 [example.com] 调试:重定向 (302) 到(引用者:www.example.com/locationB)

从 parse_item 打印出来:www.example.com/locationB

....

我认为这个问题可能与那个(推荐人:无)有关。有人能解释一下吗?

我已通过将起始 URL 更改为 www.example.com/locationB 来缩小此问题的范围。由于所有页面都包含所有位置的列表,所以这次我让我的蜘蛛开始工作:

-www.example.com/locationA

-www.example.com/locationC ...

简而言之,我正在寻找将与起始 url 相同(或被重定向)的 url 包含到 parse_item 回调将处理的列表中的方法。

【问题讨论】:

  • 您能否提供一个来自您的抓取工具的简短示例,以帮助我们更详细地了解您的问题?

标签: scrapy web-crawler


【解决方案1】:

一开始我以为有一个使用start_requests()这样简单的解决方案:

def start_requests(self):
    yield Request('START_URL_HERE', callback=self.parse_item)    

但测试表明,当使用start_requests() 而不是start_urls 列表时,spider 会忽略rules,因为没有调用CrawlSpider.parse(response)

所以,这是我的解决方案:

import itertools
class SomeSpider(CrawlSpider):
    ....
    start_urls = ('YOUR_START_URL',)
    rules = (
        Rule(
            SgmlLinkExtractor(allow=(r'YOUR_REGEXP',),),
            follow=True,
            callback='parse_item'),
        ),
    )
    def parse(self, response):
        return itertools.chain(
                     CrawlSpider.parse(self, response), 
                     self.parse_item(response))

    def parse_item(self, response):
        yield item

也许有更好的方法。

【讨论】:

    【解决方案2】:

    对于其他人也有同样的问题,经过大量搜索,您只需将您的回调函数命名为parse_start_url即可。

    例如:

    rules = (
            Rule(LinkExtractor(allow=(), restrict_xpaths=(
                '//*[contains(concat( " ", @class, " " ), concat( " ", "pagination-next", " " ))]//a',)), callback="parse_start_url", follow=True),
        )
    

    【讨论】:

      【解决方案3】:

      根据 Mindcast 建议添加示例代码:

      I manage using following approach
      
      class ExampleSpider(CrawlSpider):
      name = "examplespider"
      allowed_domains = ["example.com"]
      start_urls = ['http://example.com/A']
      
      
      rules = (Rule (SgmlLinkExtractor(restrict_xpaths=("//div[@id='tag_cloud']/a",)), callback="parse_items", follow= True),)
      
      def parse_start_url(self, response):
          self.log('>>>>>>>> PARSE START URL: %s' % response)
          # www.example.com/A will be parsed here
          return self.parse_items(response)
      
      def parse_items(self, response):
          self.log('>>>>>>>> PARSE ITEM FROM %s' % response.url)
          """Scrape data from links based on Crawl Rules"""
      

      【讨论】:

        【解决方案4】:

        一个简单的解决方法是专门为 start_urls 添加规则(在您的情况下:http://example.com/locationA)(请忽略缩进问题):

        class ExampleSpider(CrawlSpider):
          name = "examplespider"
          allowed_domains = ["example.com"]
          start_urls = ['http://example.com/locationA']
        
          rules = (
            Rule(LinkExtractor(allow=('locationA')), callback='parse_item'),
            Rule(LinkExtractor(allow=('location\.*?'),restrict_css=('.pagination-next',)), callback='parse_item', follow=True),
          )
        
          def parse_item(self, response):
               self.log('>>>>>>>> PARSE ITEM FROM %s' % response.url)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-07
          • 2021-02-19
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 2012-04-29
          相关资源
          最近更新 更多