Scrapy代码抛出TypeError:'NoneType'对象不可迭代

时间:2017-02-06 00:58:50

标签: python python-3.x scrapy

我试图请求参数site_search_url,但是当我运行时出现以下错误:``:

start_requests = iter(self.spider.start_requests())
TypeError: 'NoneType' object is not iterable

代码:

class BrickSetSpider(scrapy.Spider):
    def __init__(self, site_search_url):
        self.site_search_url = site_search_url

    def start_requests(self):
        se_base = 'http://www.se.com/search?q=site:'
        start_urls = [ se_base + self.site_search_url, ]

    def parse(self, response):
        yield scrapy.Request(
            response.urljoin(next_page),
            callback=self.parse
        )

我在这里做错了什么?

谢谢

1 个答案:

答案 0 :(得分:9)

你的start_requests什么都不返回,这意味着在Python中返回None,而它应该返回一个Request个可迭代的对象。在您的情况下,最简单的方法是在start_urls中填充__init__并且不要覆盖start_requests

class BrickSetSpider(scrapy.Spider):
    se_base = 'http://www.se.com/search?q=site:'
    def __init__(self, site_search_url):
        self.start_urls = [self.se_base + site_search_url]

    def parse(self, response):
        yield scrapy.Request(
            response.urljoin(next_page),
            callback=self.parse
        )