2016-08-07 5 views
0

내 문제는 정보가 긁어 져 데이터베이스에 표시되지 않는다는 것입니다.scrapy mysql이 빈 결과를 반환합니다.

내 스파이더는 .json 파일과 같이 정보를 잘 인쇄합니다.

settings.py에 내가

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

내 로그 공연이 오류하지만 당신은 여전히 ​​정보 수집조차 표시 불구하고 작동하는 것을 볼 수 있습니다를 추가 한

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 

과 pipelines.py 이.

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