(Python 3.6) TypeError: can't concat bytes to tuple
(Python 3.6) TypeError: can't concat bytes to tuple
我正在尝试将抓取的项目 ('title') 插入到 mySQL 数据库中。
这是我的 spider.py 文件的片段,它抓取了 'title' 内容:
#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
我正在尝试将抓取的项目 ('title') 插入到 mySQL 数据库中。
这是我的 spider.py 文件的片段,它抓取了 'title' 内容:
#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