在好不容易安装完Scrapy后尝试写一个简单的ScrapyProject,按书上的代码写完检查了很多遍保证没有错,可是在运行这个Spider的时候却出现里这个神奇的问题:
2018-08-19 18:20:15 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: example)
2018-08-19 18:20:15 [scrapy.utils.log] INFO: Versions: lxml 4.2.4.0, libxml2 2.9.5, 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:59:51) [MSC v.1914 64 bit (AMD64)], pyOpenSSL 18.0.0 (OpenSSL 1.1.0i 14 Aug 2018), cryptography 2.3.1, Platform Windows-10-10.0.17134-SP0
2018-08-19 18:20:15 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'example', 'FEED_FORMAT': 'csv', 'FEED_URI': 'books.csv', 'NEWSPIDER_MODULE': 'example.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['example.spiders']}
Traceback (most recent call last):
...
from twisted.conch import manhole, telnet
File "c:\users\pearl\appdata\local\programs\python\python37\lib\sitepackages\twisted\conch\manhole.py", line 154
def write(self, data, async=False):
^
SyntaxError: invalid syntax
manhole.py可是Python拓展包里直接下载的文件,怎么会出现SyntaxError???
只能先到"c:\users\pearl\appdata\local\programs\python\python37\lib\site-packages\“目录下把twisted\conch中的manhole.py源代码找出来看”def write(self, data, async=False)“ 这条,百度一下发现大家都说把async改成shark, 并注意把下面”def addOutput()“里的async也改成shark就OK了。结果对比:
改前:
def write(self, data, async=False):
self.handler.addOutput(data, async)
def addOutput(self, data, async=False):
if async:
self.terminal.eraseLine()
self.terminal.cursorBackward(len(self.lineBuffer) + len(self.ps[self.pn]))
self.terminal.write(data)
if async:
if self._needsNewline():
self.terminal.nextLine()
self.terminal.write(self.ps[self.pn])
if self.lineBuffer:
oldBuffer = self.lineBuffer
self.lineBuffer = []
self.lineBufferIndex = 0
self._deliverBuffer(oldBuffer)
改后:
def write(self, data, shark=False):
self.handler.addOutput(data, shark)
def addOutput(self, data, shark=False):
if shark:
self.terminal.eraseLine()
self.terminal.cursorBackward(len(self.lineBuffer) + len(self.ps[self.pn]))
self.terminal.write(data)
if shark:
if self._needsNewline():
self.terminal.nextLine()
self.terminal.write(self.ps[self.pn])
if self.lineBuffer:
oldBuffer = self.lineBuffer
self.lineBuffer = []
self.lineBufferIndex = 0
self._deliverBuffer(oldBuffer)
不难发现,代码里async被自动标位特殊颜色,因为在Python3.7里async变成了关键字,关键字是不能做变量名的,只要把这个名字改成任意不是关键字的词就好了。
Python的Extension Package里的文件看来要更新了,哈哈