Python - 从模块中导入 class 的实例
Python - import instance of class from module
我用 parse()
创建了这个 class:
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
然后我导入class
所属的module
:
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'
我错过了什么?
您正在使用字符串文字调用 parse
:
reissues = pitchfork_reissues.parse('response')
我猜那应该是一个变量名吧?像这样:
reissues = pitchfork_reissues.parse(response)
编辑
Spider 的 parse
方法需要一个 scrapy.http.Response
的实例作为第一个参数,而不是包含单词 'response'.
的字符串文字
我自己没有用过Scrapy,所以我只知道我在文档中看到的内容,但显然这样的Response实例通常是由'Downloader'创建的。
看来您正试图在 Scrapy 的常规工作流程之外调用 Spider 的 parse
方法。在那种情况下,我认为你有责任创建这样一个 Response 并在调用它的 parse
方法时将它传递给你的 Spider。
我用 parse()
创建了这个 class:
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
然后我导入class
所属的module
:
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'
我错过了什么?
您正在使用字符串文字调用 parse
:
reissues = pitchfork_reissues.parse('response')
我猜那应该是一个变量名吧?像这样:
reissues = pitchfork_reissues.parse(response)
编辑
Spider 的 parse
方法需要一个 scrapy.http.Response
的实例作为第一个参数,而不是包含单词 'response'.
我自己没有用过Scrapy,所以我只知道我在文档中看到的内容,但显然这样的Response实例通常是由'Downloader'创建的。
看来您正试图在 Scrapy 的常规工作流程之外调用 Spider 的 parse
方法。在那种情况下,我认为你有责任创建这样一个 Response 并在调用它的 parse
方法时将它传递给你的 Spider。