我试图让使用scrapy我可以得到CSV数据从Amazon的数据,但我不能够插入数据在mysql数据库 请找我的代码 我的蜘蛛如何从scrapy数据插入到mysql
import scrapy
from craigslist_sample.items import AmazonDepartmentItem
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
class AmazonAllDepartmentSpider(scrapy.Spider):
name = "amazon"
allowed_domains = ["amazon.com"]
start_urls = [
"http://www.amazon.com/gp/site-directory/ref=nav_sad/187-3757581-3331414"
]
def parse(self, response):
for sel in response.xpath('//ul/li'):
item = AmazonDepartmentItem()
item['title'] = sel.xpath('a/text()').extract()
item['link'] = sel.xpath('a/@href').extract()
item['desc'] = sel.xpath('text()').extract()
return item
我的流水线代码是
import sys
import MySQLdb
import hashlib
from scrapy.exceptions import DropItem
from scrapy.http import Request
class MySQLStorePipeline(object):
host = 'rerhr.com'
user = 'amazon'
password = 'sads23'
db = 'amazon_project'
def __init__(self):
self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db)
self.cursor = self.connection.cursor()
def process_item(self, item, spider):
try:
self.cursor.execute("""INSERT INTO amazon_project.ProductDepartment (ProductDepartmentLilnk)
VALUES (%s)""",
(
item['link'].encode('utf-8')))
self.connection.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
return item
当我运行以下命令
scrapy爬行亚马逊-o items.csv -t CSV
那么我可以能够得到的数据在我的CSV 但是当我运行
scrapy爬行亚马逊
与上面的代码我不是能够在MySQL 插入数据,请帮助我什么,我们必须做的话,我可以在MySQL中插入数据
感谢
什么是控制台?任何错误?管道是否在设置中打开?你确定你正在检查结果到你插入的同一个数据库吗?谢谢。 – alecxe 2014-12-05 15:02:09
我的管道设置是ITEM_PIPELINES = ['projectname.pipelines.MySQLStorePipeline'],是的,我正在检查相同的数据库 – wiretext 2014-12-05 15:19:24