2016-08-07 16 views
0

私の問題は、情報をスクラップしてデータベースに表示されないということです。scrapy mysqlが空の結果を返す

私のスパイダーは、.jsonファイルなどの情報を細かく印刷します。

pipelines.py

import sys 
import MySQLdb 
import hashlib 
from scrapy.exceptions import DropItem 
from scrapy.http import Request 

class MySQLStorePipeline(object): 
    def __init__(self): 
    self.conn = MySQLdb.connect(host="10.0.2.2", user='root', passwd='', db='mpmf', charset="utf8", use_unicode=True) 
    self.cursor = self.conn.cursor() 

def process_item(self, item, stack):  
    try: 
     self.cursor.execute("""INSERT INTO test (pen, name) 
        VALUES (%s, %s)""", 
        (item['pen'].encode('utf-8'), item['name'].encode('utf-8'))) 

    self.conn.commit() 


except MySQLdb.Error, e: 
    print "Error %d: %s" % (e.args[0], e.args[1]) 


return item 

とsettings.pyに私は

ITEM_PIPELINES = { 
'stack.pipelines.MySQLStorePipeline': 300, 
} 

と私のログのショーこのエラーを追加したが、あなたはまだ情報収集は、それが表示されるにもかかわらず動作することを確認できますこの。

File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 577, in _runCallbacks 
current.result = callback(current.result, *args, **kw) 
File "/root/stack/stack/pipelines.py", line 14, in process_item 
self.cursor.execute("""INSERT INTO test (pen, name) VALUES (%s, %s)""", (item['pen'].encode('utf-8'), item['name'].encode('utf-8'))) 
AttributeError: 'list' object has no attribute 'encode' 

はそう何の結果は

答えて

0

がそれを解決し、データベースにインポートされません 問題はインデントとリストオブジェクトが属性を持っていなかった

import sys 
import MySQLdb 
import hashlib 
from scrapy.exceptions import DropItem 
from scrapy.http import Request 

class MySQLStorePipeline(object): 
def __init__(self): 
self.conn = MySQLdb.connect(host="10.0.2.2", user='root', passwd='', db='mpmf', charset="utf8", use_unicode=True) 
self.cursor = self.conn.cursor() 

def process_item(self, item, stack):  
try: 
    self.cursor.execute("""INSERT INTO test (pen, name) VALUES (%s, %s)""", (item['pen'][0].encode('utf-8'), item['name'][0].encode('utf-8'))) 

    self.conn.commit() 


except MySQLdb.Error, e: 
    print "Error %d: %s" % (e.args[0], e.args[1]) 


return item 
関連する問題