gpt4 book ai didi

python - SCRAPY:如何将数据存入Mysql数据库

转载 作者:行者123 更新时间:2023-11-28 23:49:33 36 4
gpt4 key购买 nike

我在尝试将数据存储到 mysql 数据库时遇到 scrapy 问题:我收到以下错误:(screenshot here)

我在pipelines.py中的代码是

class SQLStorePipeline(object):

def __init__(self):
self.dbpool = adbapi.ConnectionPool('localhost', db='python',
user='root', passwd='', cursorclass=MySQLdb.cursors.DictCursor,
charset='utf8', use_unicode=True)

def process_item(self, item, spider):
# run db query in thread pool
query = self.dbpool.runInteraction(self._conditional_insert, item)
query.addErrback(self.handle_error)

return item

def _conditional_insert(self, tx, item):
# create record if doesn't exist.
# all this block run on it's own thread
tx.execute("select * from test where name = %s", (item['name'][0], ))
result = tx.fetchone()
if result:
log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
else:
tx.execute(\
"insert into test (name, price) "
"values (%s, %s)",
(item['link'][0],
datetime.datetime.now())
)
log.msg("Item stored in db: %s" % item, level=log.DEBUG)

def handle_error(self, e):
log.err(e)

(我从 here 中获取的)。

我的解析类是:

def parse(self, response):
item = DmozItem()
item['name'] = response.xpath('//meta[@itemprop="name"]/@content').extract()[0]
item['price'] = response.xpath('//meta[@itemprop="price"]/@content').extract()[0]
yield item

我知道这个问题已经被问过了,但是我在这里问之前尝试了所有不同的答案,但没有一个有效...

有人可以帮助我吗?提前致谢!

最佳答案

我找到了解决方案。实际上@alecxe 是对的,他的评论让我找到了解决方案。

MySQLdb 根本没有安装,原因是它安装失败,因为我的名字中有重音符号,因此 Python 无法处理路径。

再次感谢@alecxe!

关于python - SCRAPY:如何将数据存入Mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828143/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com