Scrapy 响应查询在shell中工作,但在代码中给出了一个语法错误:无效语法

Scrapy 响应查询在shell中工作,但在代码中给出了一个语法错误:无效语法,scrapy,Scrapy,如果我这样做 然后 scrapy shell https://en.wikipedia.org/wiki/Electric_battery 我得到了正确的输出 response.css('h1#firstHeading::text').extract() 但是如果我编写了这段python代码 ['Electric battery'] 我运行它,我得到了错误信息 import scrapy class WikiSpider(scrapy.Spider): name = 'wiki

如果我这样做

然后

scrapy shell https://en.wikipedia.org/wiki/Electric_battery
我得到了正确的输出

response.css('h1#firstHeading::text').extract()
但是如果我编写了这段python代码

['Electric battery']
我运行它,我得到了错误信息

import scrapy


class WikiSpider(scrapy.Spider):
    name = 'wiki'
    allowed_domains = ['wikipedia.com']
    start_urls = ['https://en.wikipedia.org/wiki/Electric_battery']

    def parse(self, response):
        print response.css('h1#firstHeading::text').extract()
C:\python\wikipedia>scrapy runspider wiki
回溯(最近一次呼叫最后一次):
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\runpy.py”,第193行,在运行模块中作为主
返回运行代码(代码、主全局、无、,
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\runpy.py”,第86行,在运行代码中
exec(代码、运行\全局)
文件“C:\Users\thega\AppData\Local\Programs\Python38-32\Scripts\scrapy.exe\\uuuuuuu main\uuuuuuuuuu.py”,第7行,在
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\cmdline.py”,执行中的第145行
cmd.crawler_process=CrawlerProcess(设置)
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\crawler.py”,第267行,在uu init中__
超级(爬虫进程,自我)。\uuuuu初始化\uuuuu(设置)
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\crawler.py”,第145行,在uu init中__
self.spider\u loader=\u get\u spider\u loader(设置)
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\crawler.py”,第347行,位于“获取蜘蛛”加载程序中
从\u设置返回加载程序\u cls.(settings.frozencopy())
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\spiderloader.py”,第61行,在from\u设置中
返回cls(设置)
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\spiderloader.py”,第25行,在u init中__
self.\u加载\u所有\u蜘蛛()
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\spiderloader.py”,第47行,在“加载所有”爬行器中
对于walk_模块中的模块(名称):
文件“c:\users\thega\appdata\local\programs\python 38-32\lib\site packages\scrapy\utils\misc.py”,第73行,在walk\u模块中
子模块=导入模块(完整路径)
文件“c:\users\thega\appdata\local\programs\python\python38-32\lib\importlib\\uuuuuu init\uuuuuu.py”,第127行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第1014行,在\u gcd\u导入中
文件“”,第991行,在“查找”和“加载”中
文件“”,第975行,在“查找”和“加载”中解锁
文件“”,第671行,在\u加载\u解锁
exec_模块中第779行的文件“”
文件“”,第916行,get_代码
文件“”,第846行,源代码为
文件“”,第219行,在“调用”中,删除了“帧”
文件“C:\python\wikipedia\wikipedia\spiders\wiki.py”,第11行
print response.css('h1#firstHeading::text').extract()
^
SyntaxError:无效语法
我从一个教程中获取代码


我对在shell中测试时语法是如何错误感到困惑???

您正在为python 2编写代码,但在python 3中运行它,您缺少括号,下面是解决方法

C:\python\wikipedia>scrapy runspider wiki
Traceback (most recent call last):
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\thega\AppData\Local\Programs\Python\Python38-32\Scripts\scrapy.exe\__main__.py", line 7, in <module>
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\cmdline.py", line 145, in execute
    cmd.crawler_process = CrawlerProcess(settings)
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\crawler.py", line 267, in __init__
    super(CrawlerProcess, self).__init__(settings)
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\crawler.py", line 145, in __init__
    self.spider_loader = _get_spider_loader(settings)
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\crawler.py", line 347, in _get_spider_loader
    return loader_cls.from_settings(settings.frozencopy())
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\spiderloader.py", line 61, in from_settings
    return cls(settings)
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\spiderloader.py", line 25, in __init__
    self._load_all_spiders()
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\spiderloader.py", line 47, in _load_all_spiders
    for module in walk_modules(name):
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\site-packages\scrapy\utils\misc.py", line 73, in walk_modules
    submod = import_module(fullpath)
  File "c:\users\thega\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 779, in exec_module
  File "<frozen importlib._bootstrap_external>", line 916, in get_code
  File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\python\wikipedia\wikipedia\spiders\wiki.py", line 11
    print response.css('h1#firstHeading::text').extract()
          ^
SyntaxError: invalid syntax

如果您使用的是Python3,那么您必须执行如下打印(数据)语句
import scrapy


class WikiSpider(scrapy.Spider):
    name = 'wiki'
    allowed_domains = ['wikipedia.com']
    start_urls = ['https://en.wikipedia.org/wiki/Electric_battery']

    def parse(self, response):
        print(response.css('h1#firstHeading::text').extract())