Scrapy Spider返回None而不是Item

the*_*ole 5 python scrapy web-scraping python-2.7

我在下面找到了答案.简而言之,ItemPipeline中的错误缩进导致它返回None.

我一直在尝试在Scrapy中编写一个CrawlSpider,之前从未使用过python.Spider爬行,调用回调函数,提取数据并填充项目,但它总是返回None.我用打印文章调用测试了它,一切都井然有序.我已经用屈服和回报尝试了这一点(虽然我仍然不明白其中的区别).坦率地说,我没有想法.下面是回调函数.//edit也添加了蜘蛛代码

class ZeitSpider(CrawlSpider):
name= xxxx
allowed_domains = ['example.com']
start_urls = ['http://www.example.com/%d/%d' %(JAHR,39)]
rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//ul[@class="teaserlist"]/li[@class="archiveteaser"]/h4[@class="title"]')),callback='parse_url',follow=True),)


    def parse_url(self,response):
        hxs = HtmlXPathSelector(response)

        article = Article()

        article['url']= response.url.encode('UTF-8',errors='strict')

        article['author']= hxs.select('//div[@id="informatives"]/ul[@class="tools"]/li[@class="author first"]/text()').extract().pop().encode('UTF-8',errors='strict')
        article['title']= hxs.select('//div[@class="articleheader"]/h1/span[@class="title"]/text()').extract().pop().encode('UTF-8',errors='strict')

        article['text']= hxs.select('//div[@id="main"]/p/text()').extract().pop().encode('UTF-8',errors='strict')

        article['excerpt'] = hxs.select('//p[@class="excerpt"]/text()').extract().pop().encode('UTF-8',errors='strict')
        yield article
Run Code Online (Sandbox Code Playgroud)

和项目定义

class Article(Item):
    url=Field()
    author=Field()
    text=Field()
    title=Field()
    excerpt=Field()
Run Code Online (Sandbox Code Playgroud)

the*_*ole 3

好的,在使用 pdb 单步执行程序后,我发现了错误:

因为我有多个蜘蛛,所以我想编写多个 ItemPipelines。为了让它们区分每个蜘蛛,我添加了一个

if spider.name=='SpiderName'
    return item
Run Code Online (Sandbox Code Playgroud)

注意缩进。Pipeline 返回 Nothing,因此输出变为 None。

改变缩进后,蜘蛛工作完美。PEBCAC 的另一个例子。