【问题标题】:scrapy mysql returns empty resultsscrapy mysql 返回空结果
【发布时间】:2016-08-07 13:49:47
【问题描述】:

所以我的问题是抓取的信息不会显示在数据库中。

我的蜘蛛可以很好地打印出信息,例如在 .json 文件中。

管道.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'

所以没有结果被导入数据库

【问题讨论】:

    标签: python mysql scrapy scrapy-pipeline


    【解决方案1】:

    解决了 问题是缩进和列表对象没有属性

    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
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 2015-07-13
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多