欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Scrapy启动报错invalid syntax的解决

程序员文章站 2022-03-24 08:13:51
目录scrapy启动报错invalid syntaxscrapy启动报错invalid syntax报错信息traceback (most recent call last): file “d:\py...

scrapy启动报错invalid syntax

报错信息

traceback (most recent call last): file “d:\python\lib\runpy.py”,
line 193, in _run_module_as_main
“main”, mod_spec) file “d:\python\lib\runpy.py”, line 85, in run_code
exec(code, run_globals) file "d:\python\scripts\scrapy.exe_main.py", line 9, in file
“d:\python\lib\site-packages\scrapy\cmdline.py”, line 150, in execute
_run_print_help(parser, _run_command, cmd, args, opts) file “d:\python\lib\site-packages\scrapy\cmdline.py”, line 90, in
_run_print_help
func(*a, **kw) file “d:\python\lib\site-packages\scrapy\cmdline.py”, line 157, in
_run_command
cmd.run(args, opts) file “d:\python\lib\site-packages\scrapy\commands\crawl.py”, line 57, in
run
self.crawler_process.crawl(spname, **opts.spargs) file “d:\python\lib\site-packages\scrapy\crawler.py”, line 170, in crawl
crawler = self.create_crawler(crawler_or_spidercls) file “d:\python\lib\site-packages\scrapy\crawler.py”, line 198, in
create_crawler
return self._create_crawler(crawler_or_spidercls) file “d:\python\lib\site-packages\scrapy\crawler.py”, line 203, in
create_crawler
return crawler(spidercls, self.settings) file “d:\python\lib\site-packages\scrapy\crawler.py”, line 55, in init
self.extensions = extensionmanager.from_crawler(self) file “d:\python\lib\site-packages\scrapy\middleware.py”, line 58, in
from_crawler
return cls.from_settings(crawler.settings, crawler) file “d:\python\lib\site-packages\scrapy\middleware.py”, line 34, in
from_settings
mwcls = load_object(clspath) file “d:\python\lib\site-packages\scrapy\utils\misc.py”, line 44, in
load_object
mod = import_module(module) file "d:\python\lib\importlib_init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level) file “

启动命令如下:

scrapy crawl first_spider -o first_spider.csv

解决方法

经过查询发现async在python3.7中是保留字,因此将twisted\conch\manhole.py中的async修改为isasync即可解决问题

使用scrapy做爬虫遇到的一些坑

使用xpath和css遇到的一些 invalid syntax

1.引号的歧义:最外面应该使用单引号

错误:

Scrapy启动报错invalid syntax的解决

正确:把定位的xpath外加单引号

Scrapy启动报错invalid syntax的解决

2.使用了关键字做变量

错误:

Scrapy启动报错invalid syntax的解决

正确:注意关键字不能做变量

Scrapy启动报错invalid syntax的解决

3.进入shell窗口时,url添加了“”

错误:

Scrapy启动报错invalid syntax的解决

正确:去掉url的引号

Scrapy启动报错invalid syntax的解决

4.使用xpath匹配相关信息,返回为空列表

错误:因为一开始网址就是错误的,所以不管匹配的xpath是不是对的,都会返回【】

Scrapy启动报错invalid syntax的解决

Scrapy启动报错invalid syntax的解决

正确:

Scrapy启动报错invalid syntax的解决

Scrapy启动报错invalid syntax的解决

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。