为什么我不能运行Python包corpcrawl的示例代码?

时间:2018-03-07 18:16:55

标签: python python-2.7 syntax-error

我安装了corpcrawl但是当我尝试从its documentation page运行此代码时:

 from corpcrawl.crawler import CorpCrawl 

from corpcrawl.backend import Backend

    def main()

我收到以下错误

from corpcrawl.crawler import CorpCrawl
Traceback (most recent call last):

  File "<ipython-input-40-a34fa6bf09cd>", line 1, in <module>
    from corpcrawl.crawler import CorpCrawl

  File "C:\ProgramData\Anaconda3\lib\site-packages\corpcrawl\crawler.py", line 1, in <module>
    from parser import Parser

ImportError: cannot import name 'Parser'

def main()
  File "<ipython-input-44-eaaf015e0d6b>", line 1
    def main()
              ^
SyntaxError: invalid syntax

为什么我收到此错误?

1 个答案:

答案 0 :(得分:0)

来自corpcrawl's documentation page的代码已损坏:

  • 缺少:
  • 错误的缩进
  • 非法字符(例如而不是"

这应该是什么样的(Python 2):

from corpcrawl.crawler import CorpCrawl
from corpcrawl.backend import Backend

class MyBackend(Backend):
    def get_company(self, name):
        pass
    def add_company(self, comp):
        print "Adding %s" % str(comp)

def main():
    my_backend = MyBackend()
    crawler = CorpCrawl(cache_path = '/an/absolute/path/to/some/dir', backend = my_backend)
    c.crawl(years = [2011, 2012], quarters = [1, 2, 3, 4])