我只是按照Scrapy文档编写了我的第一个蜘蛛。
dmoz_spider.py:
import scrapy
class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["dmoz.org"]
start_urls = [
"http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
"http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
]
def parse(self, response):
filename = response.url.split("/")[-2] + '.html'
with open(filename, 'wb') as f:
f.write(response.body)
然后当我运行它时,我收到以下错误。
E:\python2.7-64bit\Lib\site-packages\scrapy\tutorial>scrapy crawl dmoz
Traceback (most recent call last):
File "E:\python2.7-64bit\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "E:\python2.7-64bit\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "E:\python2.7-64bit\Scripts\scrapy.exe\__main__.py", line 9, in <module>
File "E:\python2.7-64bit\lib\site-packages\scrapy\cmdline.py", line 142, in execute
cmd.crawler_process = CrawlerProcess(settings)
File "E:\python2.7-64bit\lib\site-packages\scrapy\crawler.py", line 209, in __init__
super(CrawlerProcess, self).__init__(settings)
File "E:\python2.7-64bit\lib\site-packages\scrapy\crawler.py", line 115, in __init__
self.spider_loader = _get_spider_loader(settings)
File "E:\python2.7-64bit\lib\site-packages\scrapy\crawler.py", line 296, in _get_spider_loader
return loader_cls.from_settings(settings.frozencopy())
File "E:\python2.7-64bit\lib\site-packages\scrapy\spiderloader.py", line 30, in from_settings
return cls(settings)
File "E:\python2.7-64bit\lib\site-packages\scrapy\spiderloader.py", line 21, in __init__
for module in walk_modules(name):
File "E:\python2.7-64bit\lib\site-packages\scrapy\utils\misc.py", line 71, in walk_modules
submod = import_module(fullpath)
File "E:\python2.7-64bit\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "E:\python2.7-64bit\Lib\site-packages\scrapy\tutorial\tutorial\spiders\dmoz_spider.py", line 1
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
SyntaxError: invalid syntax
我该如何解决?在Python 2.7-32bit上运行它?
答案 0 :(得分:1)
您的文件以文字行开头:
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
删除该行。