0

私は完全に迷っています。これが私のパイプラインです。実行すると、次のエラーが表示されます

      File "c:\python27\lib\site-packages\twisted\internet\defer.py", line 588, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:\Python27\bff\bff\pipelines.py", line 42, in process_item
    cursor.execute(add_Product)
  File "c:\python27\lib\site-packages\mysql\connector\cursor.py", line 492, in execute
    stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'

コメント付きのコードからわかるように、いくつかの方法を試しました。最初は、例で見たとおりに実行していましたが、上記で Name = item として定義するのではなく、VALUES 行に (item['StoreName']) を入力すると、sadi item が定義されていないというエラーが発生しました。 ['StoreName'] mqsql.org Web サイトからインストールした mySQL.connector を使用しています。前もって感謝します

# -*- coding: utf-8 -*-
# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector  
#from scrapy.extensions import DropItem
#from bff.items import ItemInfo

class mySQLPipeline(object):
def process_item(self, item, spider):

    Path = item['ProdPath']
    UPC = item['ProdUPC']
    Model = item['ProdModel']
    Desc = item['ProdDesc']
    Price = item['ProdPrice']
    Stock = item['InStock']
    #Ships = item['Ships']
    Name = item['StoreName']


    cnx = mysql.connector.connect(user='*****', password='*****',
                                  host='127.0.0.1',
                                  port='****',
                                  database='scrapyinfo')
    cursor = cnx.cursor()

    #add_Product = ("INSERT INTO walmart_products (ProdName)"
    #               "VALUES (%s), (Name);")

    add_Product = ("INSERT INTO walmart_products, (ProdName, ProdPath, ProdUPC, ProdModel, ProdDesc, ProdPrice, InStock, Ships, StoreName)"
                   "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (Name, Path, UPC, Model, Desc, Price, Stock, Name))
                    #item['Ships'],



    #Add new product
    cursor.execute(add_Product)

    # Make sure data is committed to the database
    cnx.commit()

    cursor.close()
    cnx.close()
    return item

編集。これが私の新しいコードです

`

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector  
#from scrapy.extensions import DropItem
#from bff.items import ItemInfo

class mySQLPipeline(object):
    def process_item(self, item, spider):

        Product = item['ProdName']      
        Path = item['ProdPath']
        UPC = item['ProdUPC']
        Model = item['ProdModel']
        Desc = item['ProdDesc']
        Price = item['ProdPrice']
        Stock = item['InStock']
        #Ships = item['Ships']
        Name = item['StoreName']


        cnx = mysql.connector.connect(user='****', password='****',
                                      host='127.0.0.1',
                                      port='****',
                                      database='****')
        cursor = cnx.cursor()
 #       add_Product = ("INSERT INTO walmart_products (ProdName, StoreName) VALUES (%s, %s,)", Product, Name,)
 #       add_Product = ("INSERT INTO walmart_products, (ProdName)"
 #                      "VALUES (%s)", (Name))
 #                      "VALUES (%(Name)s)")
        add_Product = ("INSERT INTO walmart_products "
                        "(ProdName, ProdPath, ProdUPC, ProdModel, ProdDesc, ProdPrice, InStock, StoreName) "
                        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)")
                        #item['Ships'],

        data_Product = (Product, Path, UPC, Model, Desc, Price, Stock, Name)                                        

        #Add new product
        cursor.execute(add_Product, data_Product)

        # Make sure data is committed to the database
        cnx.commit()

        cursor.close()
        cnx.close()
        return item

`

4

1 に答える 1

0

誰かが私がいた場所で立ち往生している場合に備えて、私はそれを理解できませんでした。私はイライラして mysql.connector の使用をやめ、代わりに MySQLdb を使用しました。それに切り替えると、すべてがうまくいきました。

于 2016-01-10T20:05:08.743 に答える