我是 Python 新手,在 eclipse 中使用 PyDev 成功安装了 Scrapy。当我运行程序时,它显示如下(如图所示)
[ ]
我正在运行这段代码:
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):
for sel in response.xpath('//ul/li'):
title = sel.xpath('a/text()').extract()
link = sel.xpath('a/@href').extract()
desc = sel.xpath('text()').extract()
print title, link, desc
这是什么?无法运行程序。
请您参考如下方法:
您的当前目录不是Scrapy项目。
scrapy 项目具有定义的格式和文件。看看:http://doc.scrapy.org/en/latest/intro/tutorial.html
你真的应该把教程看一遍。
基本上,Scrapy 项目的目录结构如下:
tutorial/
scrapy.cfg # deploy configuration file
tutorial/ # project's Python module, you'll import your code from here
__init__.py
items.py # project items file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
...
要创建一个 scrapy 项目,请转到您的项目文件夹并运行:
scrapy startproject projectname
创建项目后,您现在可以从项目根文件夹运行 scrapy。运行 scrapy 时,请确保您位于项目的根目录下。
