MSSQL 的 Scrapy 管道 [英] Scrapy pipeline for MSSQL

查看:77
本文介绍了MSSQL 的 Scrapy 管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Python scrapy 编写自己的管道:

I am writing my own pipeline for Python scrapy:

from scrapy.exceptions import NotConfigured
from scrapy.exceptions import DropItem
import pymssql

from slybot.item import create_item_version

class SQLStore(object):
  def __init__(self):
    self.conn = pymssql.connect(host='XXXXXX', user='sa', password='1timep', database='DBSample')
    self.cursor = self.conn.cursor()
    #log data to json file


def process_item(self, item, spider): 

    try:
        self.cursor.execute("INSERT INTO Movie(Description, Location,Title) VALUES (%s, %s, %s)", (item['Description'], item['Location'], item['Title']))
        self.conn.commit()

    except pymssql.Error, e:
        print ("error")

        return item

我正在尝试将值插入 SQL 服务器.

I am trying to insert the values into SQL server.

以下是我的蜘蛛设置:

ITEM_PIPELINES = {'slybot.dupefilter.SQLStore' : 100}

它工作正常.当我在 Scrapyd 中提交我的蜘蛛时,我看到了下面的日志文件

It is working fine. And when i submit my spider in Scrapyd i am seeing the below log file

2015-01-19 16:07:57+0530 [scrapy] INFO: Enabled item pipelines: SQLStore

从日志文件中,我看到我的蜘蛛正在使用 SQLStore 管道.

From the log file i am seeing that my spider is using the SQLStore pipline.

但是 值没有加载到 SQL 服务器 .我能够以json格式查看日志文件中的内容.

But the values are not loaded into SQL server . I am able to see the content in the log files in the json format.

出了什么问题.问题是什么?

What went wrong. And what is the problem?

谁能帮帮我?谢谢.

推荐答案

代码缩进不正确.process_itemSQLStore 类定义处于同一级别,因此它不是类的方法,永远不会被调用.缩进:

The code is not properly indented. process_item is on the same level as SQLStore class definition, hence it is not a method of a class and is never called. Indent it:

import pymssql

from slybot.item import create_item_version


class SQLStore(object):
    def __init__(self):
        self.conn = pymssql.connect(host='XXXXXX', user='sa', password='1timep', database='DBSample')
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        try:
            self.cursor.execute("INSERT INTO Movie(Description, Location,Title) VALUES (%s, %s, %s)",
                                (item['Description'], item['Location'], item['Title']))
            self.conn.commit()
        except pymssql.Error, e:
            print ("error")

        return item

这篇关于MSSQL 的 Scrapy 管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆