簡體   English   中英

Python-從模塊導入類的實例

[英]Python - import instance of class from module

我已經用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

然后我導入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實例通常是由“下載器”創建的。

似乎您正在嘗試在Scrapy的常規工作流程之外調用Spider的parse方法。 在那種情況下,我認為您有責任創建這樣的Response,並在調用它的parse方法時將其傳遞給Spider。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM