【问题标题】:Scrapy won't crawl the urlScrapy不会抓取网址
【发布时间】:2020-09-05 10:18:54
【问题描述】:

我正在尝试制作一个简单的scrapy 程序来从http://quotes.toscrape.com/ 网站上抓取报价。输出应存储在 html 文件中。但是当我运行代码时,它不会输出任何东西。终端显示它爬取了 0 个页面 Terminal output

这是以下代码。你能帮我解决一下有什么问题吗谢谢

import scrapy

class SimpleSpider(scrapy.Spider):
    name ="SimpleSpider"
    
    def start_request(self):
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
            ]
        
        for url in urls:
            yield scrapy.Request(url, self.parse)
            
    def parse(self, response):
        page = response.url.split('/')[-1]
        filename = 'quotes-%s.html' % page
        with open(filename, 'wb') as f:
            f.write(response.body)
        self.log('Files saved to %s' % filename)

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    我想这只是一个命名问题。 使用start_requests 而不是start_request

    见:https://docs.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_requests

    【讨论】:

      【解决方案2】:

      请检查一下,它对我有用。请遵循标准的scrapy spider。

      import scrapy
      
      
      class SimpleSpider(scrapy.Spider):
          name = "SimpleSpider"
          start_urls = [
              'http://quotes.toscrape.com/page/1/',
              'http://quotes.toscrape.com/page/2/',
          ]
      
          def start_requests(self):
      
              for url in self.start_urls:
                  yield scrapy.Request(url, self.parse)
      
          def parse(self, response):
              page = response.url.split('/')[-1]
              filename = 'quotes-%s.html' % page
              with open(filename, 'wb') as f:
                  f.write(response.body)
              self.log('Files saved to %s' % filename)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多