2017-06-28 128 views
1

我正試圖在mySQL數據庫中插入scraped items('title')。(Python 3.6)TypeError:無法將字節連接到元組

這是我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 

這是什麼類型錯誤:不能Concat的字節元組是什麼意思?它應該如何解決?

+0

如果你使用:'SQL = 「INSERT INTO項目(標題)VALUES(」 +項目[ '標題'] +「)」'而不是? – nikpod

+0

'sql =「INSERT INTO items(title)VALUES({0})」。format(item ['title'])' – user3450049

+0

@ user3450049 - 你能澄清{0}的用途嗎?我嘗試了你的建議並得到了這個錯誤:pymysql.err.ProgrammingError:(1064,「你的SQL語法有錯誤;檢查與你的MariaDB服務器版本相對應的手冊, s/s電器 - h/w地板 - 可用:7月1日在1號線) 作爲參考,'標題'scraped讀取如下:'AAA主要位置 - s/s電器 - h/w地板 - Avail :7月1日'。 – slsu

回答

0

這是能夠最終將數據插入到數據庫中的語法:

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 
相關問題