我需要处理scrapy的输出并使用mysqldb将其存储到mysql。下面的代码适用于文章表但是我需要访问category_id以作为外键插入articlecategory表。项目['频道']是一个包含类别的列表。 但是,下面的输出只返回id:1,它不会退出。
def process_item(self, item, spider):
try:
self.cursor.execute(\
"insert into article (url, pid , date)\
values (%s, %s, %s)",
(item['url'], item['pid'], item['pub_date'])
)
article_id = self.cursor.lastrowid
self.conn.commit()
for channel in item['channels']:
self.cursor.execute("insert ignore into category(category_name) values(%s)",([channel]))
self.conn.commit()
category_id = self.cursor.execute("select id from category where category_name = %s", ([channel]))
with open("cat.txt", "a") as f:
f.write("id: %s \n" % (category_id))
如何访问与类别名称匹配的类别ID?该语法适用于带类别表的插入操作。
答案 0 :(得分:0)
解决了这个问题,不得不做fetchall()
self.cursor.execute("select id from category where category_name = %s", ([channel]))
category_id = self.cursor.fetchall()
with open("cat.txt", "a") as f:
f.write("id: %s \n" % (category_id[0]))