gpt4 book ai didi

python - (Python 3.6) 类型错误 : can't concat bytes to tuple

转载 作者:行者123 更新时间:2023-11-30 21:57:03 36 4
gpt4 key购买 nike

我正在尝试将抓取的项目(“标题”)插入到 mySQL 数据库中。

这是抓取“标题”内容的 spider.py 文件片段:

#get more details
def parse_detail_page(self, response):
item = response.meta["item"]

#title
title = response.xpath(".//*[@id='titletextonly']/text()").extract()[0]
item["title"] = title

return item

这是我的 pipelines.py 文件:

import pymysql.cursors

class mySQLTest(object):

def __init__(self):
self.conn = pymysql.connect(host='localhost',
user='root',
password='',
db='testDB',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()


def process_item (self, item, spider):
sql = "INSERT INTO items (title) VALUES (%s)", (item['title'])
self.cursor.execute(sql)
self.conn.commit()

return item

我遇到了以下错误:

    Traceback (most recent call last):
File "/Users/venv1/lib/python3.6/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/venv1/tutorial/tutorial/pipelines.py", line 31, in process_item
self.cursor.execute(sql)
File "/Users/venv1/lib/python3.6/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/Users/sandysu/PyCharmProjects/venv1/lib/python3.6/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/Users/venv1/lib/python3.6/site-packages/pymysql/connections.py", line 855, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/Users/venv1/lib/python3.6/site-packages/pymysql/connections.py", line 1091, in _execute_command
packet = prelude + sql[:packet_size-1]
TypeError: can't concat bytes to tuple

TypeError: can't concat bytes to tuple 是什么意思?应该如何修复?

最佳答案

这是最终能够将数据插入数据库的语法:

def process_item (self, item, spider):
sql = "INSERT INTO items (title) VALUES (%s)"
data = item['title']

self.cursor.execute(sql, data)
self.conn.commit()

return item

关于python - (Python 3.6) 类型错误 : can't concat bytes to tuple,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793792/

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