【发布时间】:2013-01-25 17:21:19
【问题描述】:
我四处寻找答案,但找不到答案。正如我昨天提到的,我是scrapy和python的新手,所以答案可能就在那里,但我没有赶上。
我写了我的蜘蛛,它工作得很好。这是我的管道....
import sys
import MySQLdb
import hashlib
from scrapy.exceptions import DropItem
from scrapy.http import Request
class somepipeline(object):
def __init__(self):
self.conn = MySQLdb.connect(user='user', 'passwd', 'dbname', 'host', charset="utf8", use_unicode=True)
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
try:
self.cursor.execute("""INSERT INTO sometable (title, link, desc)
VALUES (%s, %s)""",
(item['title'].encode('utf-8'),
item['link'].encode('utf-8'),
item['desc'].encode('utf-8'))
self.conn.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
return item
这是我的设置:
BOT_NAME = 'somebot'
SPIDER_MODULES = ['somespider.spiders']
NEWSPIDER_MODULE = 'somespider.spiders'
ITEM_PIPELINES = ['myproject.pipeline.somepipeline']
但是,当我运行它时,我得到: 没有名为管道的模块错误
找到了一个类似的答案,但它是针对图像类的,我只想要 HTML 数据。
我做错了什么?我需要下载另一个模块吗?感谢帮助。如果我很近,请给我一个肘部。
【问题讨论】:
-
这个文件在哪里?它是否包含在
myproject/和pipeline的/path/to/somewhere/myproject/pipeline'? And are these pacakages, i.e. are there__init__.py` 文件中(如果管道是一个目录)? -
路径是 projectdirectory/project(with scrappy.cfg)/ 里面有 pipeline.py 和所有预期的文件以及 .pyc 文件。根据另一篇文章,我删除了 .pyc 文件并再次运行它。同样的问题。
-
如果您正在运行的脚本在
projectdirectory/project中,那么 ITEM_PIPELINES 的正确名称应该是pipeline.somepipeline并且pipeline目录应该有__init__.py文件。看来你应该输入 python 包的路径,阅读它。 -
我的 init.py 文件是空的。那里应该有东西吗?那是我需要调查的文件吗?还是python包的路径?