第一次使用Scrapy就遇到了一个twisted语法错误问题,按理说其实不应该这样,下面记录详细的过程和解决方法
环境:windows10+Python3.7
D:\PythonWorkerspace>scrapy startproject xdb
New Scrapy project 'xdb', using template directory 'c:\\python3.7\\lib\\site-packages\\scrapy\\templates\\project', created in:
D:\PythonWorkerspace\xdb
You can start your first spider with:
cd xdb
scrapy genspider example example.com
D:\PythonWorkerspace>cd xdb
D:\PythonWorkerspace\xdb>dir
驱动器 D 中的卷是 新加卷
卷的序列号是 90B7-EFCF
D:\PythonWorkerspace\xdb 的目录
2018/10/13 06:29
D:\PythonWorkerspace\xdb>scrapy genspider chouti dig.chouti.com
Created spider 'chouti' using template 'basic' in module:
xdb.spiders.chouti
D:\PythonWorkerspace\xdb>scrapy genspider cnblogs cnblogs.com
Created spider 'cnblogs' using template 'basic' in module:
xdb.spiders.cnblogs
因为是第一次使用爬虫,所以在这里的parse方法里只写了一行代码:
print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
在项目根目录下输入scrapy crawl chouti启动这个爬虫
D:\PythonWorkerspace\xdb>scrapy crawl chouti
2018-10-13 07:13:46 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: xdb)
2018-10-13 07:13:46 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.7, cssselect 1.0.3, parsel 1.5.0, w3lib 1.19.0
, Twisted 18.7.0, Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)], pyOpenSSL 18.0.0 (OpenSS
L 1.1.0i 14 Aug 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-SP0
2018-10-13 07:13:46 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'xdb', 'NEWSPIDER_MODULE': 'xdb.spiders', 'ROBOTST
XT_OBEY': True, 'SPIDER_MODULES': ['xdb.spiders']}
Traceback (most recent call last):
File "c:\python3.7\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python3.7\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python3.7\Scripts\scrapy.exe\__main__.py", line 9, in
File "c:\python3.7\lib\site-packages\scrapy\cmdline.py", line 150, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "c:\python3.7\lib\site-packages\scrapy\cmdline.py", line 90, in _run_print_help
func(*a, **kw)
File "c:\python3.7\lib\site-packages\scrapy\cmdline.py", line 157, in _run_command
cmd.run(args, opts)
File "c:\python3.7\lib\site-packages\scrapy\commands\crawl.py", line 57, in run
self.crawler_process.crawl(spname, **opts.spargs)
File "c:\python3.7\lib\site-packages\scrapy\crawler.py", line 170, in crawl
crawler = self.create_crawler(crawler_or_spidercls)
File "c:\python3.7\lib\site-packages\scrapy\crawler.py", line 198, in create_crawler
return self._create_crawler(crawler_or_spidercls)
File "c:\python3.7\lib\site-packages\scrapy\crawler.py", line 203, in _create_crawler
return Crawler(spidercls, self.settings)
File "c:\python3.7\lib\site-packages\scrapy\crawler.py", line 55, in __init__
self.extensions = ExtensionManager.from_crawler(self)
File "c:\python3.7\lib\site-packages\scrapy\middleware.py", line 58, in from_crawler
return cls.from_settings(crawler.settings, crawler)
File "c:\python3.7\lib\site-packages\scrapy\middleware.py", line 34, in from_settings
mwcls = load_object(clspath)
File "c:\python3.7\lib\site-packages\scrapy\utils\misc.py", line 44, in load_object
mod = import_module(module)
File "c:\python3.7\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "
File "
File "
File "
File "
File "
File "c:\python3.7\lib\site-packages\scrapy\extensions\telnet.py", line 12, in
from twisted.conch import manhole, telnet
File "c:\python3.7\lib\site-packages\twisted\conch\manhole.py", line 154
def write(self, data, async=False):
^
SyntaxError: invalid syntax
D:\PythonWorkerspace\xdb>
居然这里抛出了异常:SyntaxError: invalid syntax----语法错误,
在Pycharm 里面打开 c:\python3.7\lib\site-packages\twisted\conch\manhole.py (按Ctrl键+鼠标左键)打开后是这样:
在154行这里提示:formal parameter name expected,查询发现self.handler.addOutput()里面也是用的同样的形参。
其实这里的async是python里面的关键字,使用关键字作为形参名称(本质上还是变量)自然是非法的。
明白了错误的原因后解决问题就很方便了,修改一下这个形参的名称,例如我修改成_ansyc,然后保存文件
重启启动这个爬虫,运行正常。