0
类的进口比如我创造了这个类parse()
:Python的 - 从模块
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'
我错过了什么?
给了我下面的回溯:'reissues = pitchfork_reissues.parse(response) NameError:全局名称'response'未定义' –
那么你需要一个scrapy.http.Response实例,它显式地被“下载由下载者)“。参见[文档](http://doc.scrapy.org/en/latest/topics/request-response.html#response-objects):) – Jasper