我用parse()创建了这个类
class PitchforkSpider(scrapy.Spider):
name = "pitchfork_reissues"
allowed_domains = ["pitchfork.com"]
#creates objects for each URL listed here
start_urls = [
"http://pitchfork.com/reviews/best/reissues/?page=1",
"http://pitchfork.com/reviews/best/reissues/?page=2",
"http://pitchfork.com/reviews/best/reissues/?page=3",
]
def parse(self, response):
for sel in response.xpath('//div[@class="album-artist"]'):
item = PitchforkItem()
item['artist'] = sel.xpath('//ul[@class="artist-list"]/li/text()').extract()
item['reissue'] = sel.xpath('//h2[@class="title"]/text()').extract()
return item然后导入module,其中class属于:
from blogs.spiders.pitchfork_reissues_feed import *并尝试在另一个上下文中调用parse():
def reissues(self):
pitchfork_reissues = PitchforkSpider()
reissues = pitchfork_reissues.parse('response')
print (reissues)但我得到了以下错误:
pitchfork_reissues.parse('response')
File "/Users/vitorpatalano/Documents/Code/Soup/Apps/myapp/blogs/blogs/spiders/pitchfork_reissues_feed.py", line 21, in parse
for sel in response.xpath('//div[@class="album-artist"]'):
AttributeError: 'str' object has no attribute 'xpath'我遗漏了什么?
发布于 2016-09-23 23:00:55
您使用字符串文字调用parse:
reissues = pitchfork_reissues.parse('response')我想那应该是个可变的名字吧?就像这样:
reissues = pitchfork_reissues.parse(response)编辑
蜘蛛的parse方法需要一个scrapy.http.Response的实例,作为它的第一个参数,而不是包含单词'response‘的字符串文本。
我自己也没有使用Scrapy,所以我只知道我在文档中看到了什么,但是很明显,这样的响应实例通常是由'Downloader‘创建的。
看起来,您试图在Scrapy通常的工作流之外调用您的Spider的parse方法。在这种情况下,我认为您应该负责创建这样一个响应,并在调用它是parse方法时将其传递给您的蜘蛛。
https://stackoverflow.com/questions/39670621
复制相似问题