mysql 垃圾_垃圾mysql pipelin

我正在使用scrapy,并试图将从spider抓取的数据保存到MySql数据库中。我正在使用管道来实现这一点,但没有运气。下面是我的管道代码:from scrapy import log

from scrapy.core.exceptions import DropItem

from twisted.enterprise import adbapi

import time

import MySQLdb.cursors

class FilterWordsPipeline(object):

"""A pipeline for filtering out items which contain certain words in their

description"""

# put all words in lowercase

words_to_filter = ['politics', 'religion']

def process_item(self, spider, item):

print spider

for word in self.words_to_filter:

if word in unicode(item['description']).lower():

raise DropItem("Contains forbidden word: %s" % word)

else:

return item

class MySQLStorePipeline(object):

def __init__(self):

# @@@ hardcoded db settings

# TODO: make settings configurable through settings

self.dbpool = adbapi.ConnectionPool('adress_to_db',

db='my_db',

user='my_user',

passwd='my_pw',

cursorclass=MySQLdb.cursors.DictCursor,

charset='utf8',

use_unicode=True

)

def process_item(self, spider, item):

# run db query in thread pool

query = self.dbpool.runInteraction(self._conditional_insert, item)

query.addErrback(self.handle_error)

return item

def _conditional_insert(self, tx, item):

# create record if doesn't exist.

# all this block run on it's own thread

tx.execute("select * from scrapytest where link = %s", (item['link'][0], ))

result = tx.fetchone()

if result:

log.msg("Item already stored in db: %s" % item, level=log.DEBUG)

else:

tx.execute(\

"insert into scrapytest (title, link, desc) "

"values (%s, %s, %s)",

(item['title'][0],

item['link'][0],

item['desc'][0]

)

log.msg("Item stored in db: %s" % item, level=log.DEBUG)

def handle_error(self, e):

log.err(e)

我得到的错误信息是:

^{pr2}$

我不知道从哪里开始,所以非常感谢你的帮助!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值