python - 将Scrapy数据保存到MySQL中对应的URL

标签 python mysql scrapy screen-scraping

目前正在使用 Scrapy。

我有一个存储在 MySQL 数据库中的 URL 列表。蜘蛛访问这些 URL,捕获两个目标信息(scorecount)。我的目标是当 Scrapy 完成抓取时,它会在移动到下一个 URL 之前自动填充相应的列。

我是新手,我似乎无法让保存部分正常工作。 scorecount 已成功传递到数据库。但它保存为新行而不是与源 URL 相关联。

这是我的代码: amazon_spider.py

import scrapy
from whatoplaybot.items import crawledScore
import MySQLdb

class amazonSpider(scrapy.Spider):
    name = "amazon"
    allowed_domains = ["amazon.com"]
    start_urls = []

    def parse(self, response):
        print self.start_urls

    def start_requests(self):
        conn = MySQLdb.connect(
                user='root',
                passwd='',
                db='scraper',
                host='127.0.0.1',
                charset="utf8",
                use_unicode=True
                )
        cursor = conn.cursor()
        cursor.execute(
            'SELECT url FROM scraped;'
            )

        rows = cursor.fetchall()

        for row in rows:
            yield self.make_requests_from_url(row[0])
        conn.close()

    def parse(self, response):
        item = crawledScore()
        item['reviewScore'] = response.xpath('//*[@id="avgRating"]/span/a/span/text()').re("[0-9,.]+")[0]
        item['reviewCount'] = response.xpath('//*[@id="summaryStars"]/a/text()').re("[0-9,]+")
        yield item

管道.py

import sys
import MySQLdb

class storeScore(object):
    def __init__(self):
        self.conn = MySQLdb.connect(
            user='root',
            passwd='',
            db='scraper',
            host='127.0.0.1',
            charset="utf8",
            use_unicode=True
        )
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        try:
            self.cursor.execute("""INSERT INTO scraped(score, count) VALUES (%s, %s)""", (item['reviewScore'], item['reviewCount']))
            self.conn.commit()

        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])

            return item

非常感谢任何帮助和指导。

谢谢你们。

最佳答案

按照以下步骤:

reviewURL 字段添加到您的 crawledScore 项目中:

class crawledScore(scrapy.Item):
    reviewScore = scrapy.Field()
    reviewCount = scrapy.Field()
    reviewURL = scrapy.Field()

响应 url 保存到项目 ['reviewURL'] 中:

def parse(self, response):
    item = crawledScore()
    item['reviewScore'] = response.xpath('//*[@id="avgRating"]/span/a/span/text()').re("[0-9,.]+")[0]
    item['reviewCount'] = response.xpath('//*[@id="summaryStars"]/a/text()').re("[0-9,]+")
    item['reviewURL'] = response.url
    yield item

最后,在您的管道文件中,根据您的逻辑插入或更新:

插入:

def process_item(self, item, spider):
    try:
        self.cursor.execute("""INSERT INTO scraped(score, count, url) VALUES (%s, %s, %s)""", (item['reviewScore'], item['reviewCount'], item['reviewURL']))
        self.conn.commit()
    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])

        return item

更新:

def process_item(self, item, spider):
        try:
            self.cursor.execute("""UPDATE scraped SET score=%s, count=%s WHERE url=%s""", (item['reviewScore'], item['reviewCount'], item['reviewURL']))
            self.conn.commit()
        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])

            return item

关于python - 将Scrapy数据保存到MySQL中对应的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32670383/

相关文章:

python - Scrapy:仅关注外部链接

python - 如何从亚马逊产品页面中提取 asin

python - numpy: "array_like"对象的正式定义?

python - 理解 Python 类的继承

python - 查找数据框中行的最大值并在 pandas 中返回其列名称

php - mysql_num_rows PHP

python - 使用 Python 和 Pygame 进行贪吃蛇

mysql - 索引创建优化

mysql - 显示具有相同用户 'tags' 或 'categories' 的所有帖子

python 和 scrapy 编码问题