2017-01-07 5 views
0

にインポートできません。現在、Scrapese.pyという名前で保存されているPythonクラスをどのようにインポートできますか?import私のスクラピースクリプトは、メソッド

from Scrapese import parse 

が、それは言う::

import scrapy 

class Scrapese(scrapy.Spider): 
    name = 'scrape-se' 
    seach_engine = [ 
     'se1.com', 
     'se2.com', 
    ] 

    def parse(self, seach_engine, site_to_parse, page_start, page_end, response): 
     site = str(seach_engine+site_to_parse) 
     if site_to_parse == seach_engine[0]: 
      print("executing against se1!") 
     elif site_to_parse == searh_engine[1]: 
      print("executing against se2!") 
     else: 
      print("Something bad happened.") 

私は典型的に努力を続ける

ImportError: cannot import name 'parse' 

は私が間違って何をしているのですか?

おかげ

+1

'Scrapese.parse'を直接インポートすることはできませんが、' from Scrapese import Scrapese'を使用してクラスをインポートし、そのクラスのインスタンスを作成し、 'parse'を呼び出します。 – Tagc

答えて

1

ScrapeseはあなたにもScrapeseというクラスを定義するPythonモジュールの名前です。

from Scrapese import parseは、Scrapeseというモジュールをインポートして、オブジェクトparseを探します。

# Scrapese.py 

class Scrapese(object): 
    def parse(self): 
     pass 

# main.py 

from Scrapese import Scrapese 

o = Scrapese() 
o.parse() 

これあなたがインスタンス化と解析のために使用することができます別のスクリプト(main.py)、内Scrapeseクラス定義を利用できるようにPythonインタプリタが発生します。

あなたはおそらくやりたいことは以下のとおりです。 。

+0

必要に応じてファイルscrapeseを呼び出すだけならどうですか? – Jshee

+0

@Jsheeあなたはどういう意味ですか? – Tagc

+0

Nvm、私はあなたがこれを明確にしたと思う。ありがとう@タッグ! – Jshee

関連する問題