【问题标题】:Why am I getting this error in scrapy - python3.7 invalid syntax为什么我在scrapy中收到此错误-python3.7无效语法
【发布时间】:2018-02-19 07:25:25
【问题描述】:

我在安装scrapy 时玩得很开心。我已将它安装在我的 Mac 上,但在运行教程时出现此错误:

Virtualenvs/scrapy_env/lib/python3.7/site-packages/twisted/conch/manhole.py", line 154
    def write(self, data, async=False):
                              ^
SyntaxError: invalid syntax

据我所知,我使用的是最新版本。启动并运行它一直很痛苦。嘘。 操作系统 High Sierra 10.13.3 蟒蛇3.7 安装了 ipython 我已经更新了我能想到的一切。 终端线是:

scrapy shell http://quotes.toscrape.com/random

scrapy shell "http://quotes.toscrape.com/random" 甚至尝试过单引号。 任何帮助都会很棒!

这是完整的日志:

(scrapy_env) XXX-MacBook-Pro:Virtualenvs ComputerName$ scrapy shell http://quotes.toscrape.com/random
2018-02-19 06:38:53 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot: scrapybot)
2018-02-19 06:38:53 [scrapy.utils.log] INFO: Versions: lxml 3.6.2.0, libxml2 2.9.4, cssselect 1.0.3, parsel 1.4.0, w3lib 1.19.0, Twisted 17.9.0, Python 3.7.0a2 (v3.7.0a2:f7ac4fe52a, Oct 16 2017, 21:11:18) - [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)], pyOpenSSL 17.5.0 (OpenSSL 1.0.2n  7 Dec 2017), cryptography 2.1.4, Platform Darwin-17.4.0-x86_64-i386-64bit
2018-02-19 06:38:53 [scrapy.crawler] INFO: Overridden settings: {'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter', 'LOGSTATS_INTERVAL': 0}
Traceback (most recent call last):
  File "/Users/ComputerName/Virtualenvs/scrapy_env/bin/scrapy", line 11, in <module>
    load_entry_point('Scrapy==1.5.0', 'console_scripts', 'scrapy')()
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/cmdline.py", line 150, in execute
    _run_print_help(parser, _run_command, cmd, args, opts)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/cmdline.py", line 90, in _run_print_help
    func(*a, **kw)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/cmdline.py", line 157, in _run_command
    cmd.run(args, opts)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/commands/shell.py", line 65, in run
    crawler = self.crawler_process._create_crawler(spidercls)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/crawler.py", line 203, in _create_crawler
    return Crawler(spidercls, self.settings)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/crawler.py", line 55, in __init__
    self.extensions = ExtensionManager.from_crawler(self)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/middleware.py", line 58, in from_crawler
    return cls.from_settings(crawler.settings, crawler)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/middleware.py", line 34, in from_settings
    mwcls = load_object(clspath)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/utils/misc.py", line 44, in load_object
    mod = import_module(module)
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 680, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/Scrapy-1.5.0-py3.7.egg/scrapy/extensions/telnet.py", line 12, in <module>
    from twisted.conch import manhole, telnet
  File "/Users/ComputerName/Virtualenvs/scrapy_env/lib/python3.7/site-packages/twisted/conch/manhole.py", line 154
    def write(self, data, async=False):
                              ^
SyntaxError: invalid syntax
(scrapy_env) XXX-MacBook-Pro:Virtualenvs ComputerName$

【问题讨论】:

    标签: python python-3.x macos scrapy-shell


    【解决方案1】:

    你可以的

    pip install git+https://github.com/twisted/twisted.git@trunk
    

    因为“主干”分支已经合并了修复“9384-remove-async-param”

    你可以在https://github.com/twisted/twisted/pull/966看到

    【讨论】:

      【解决方案2】:

      原来 async 和 await 是 python 3.7 中的关键字。这就是语法错误的原因。这是来自用户 stranac。安装了针对 2.7 的新 virtualenv,安装 scrapy 或运行 bench/shell 没有问题。

      【讨论】:

      【解决方案3】:

      您可以在扭曲文件中将 async 替换为 isAsync。

      转到 Python37\Lib\site-packages\twisted\conch 编辑“manhole”文件并将“async”参数替换为“isAsync”。

      【讨论】:

      • async 是 python 3.7 中的保留字
      • 感谢您的建议!当我有机会回到这个问题时,我会尝试。尼克
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2020-09-04
      • 2022-01-23
      • 2011-10-08
      相关资源
      最近更新 更多