【问题标题】:Scrapy Pipeline doesn't insert into MySQLScrapy Pipeline 不会插入 MySQL
【发布时间】:2017-09-25 03:37:36
【问题描述】:

我正在尝试使用 Scrapy 为大学项目构建一个小型应用程序。 蜘蛛正在抓取项目,但我的管道没有将数据插入 mysql 数据库。为了测试管道是否不起作用或 pymysl 实现是否不起作用,我编写了一个测试脚本:

代码开始

#!/usr/bin/python3

import pymysql

str1 = "hey"
str2 = "there"
str3 = "little"
str4 = "script"

db = pymysql.connect("localhost","root","**********","stromtarife" )

cursor = db.cursor()

cursor.execute("SELECT * FROM vattenfall")
cursor.execute("INSERT INTO vattenfall (tarif, sofortbonus, treuebonus, jahrespreis) VALUES (%s, %s, %s, %s)", (str1, str2, str3, str4))
cursor.execute("SELECT * FROM vattenfall")
data = cursor.fetchone()
print(data)
db.commit()
cursor.close()

db.close()

代码结束

运行此脚本后,我的数据库有一条新记录,因此它不是我的 pymysql.connect() 函数,它已损坏。

我会提供我的scrapy代码:

vattenfall_form.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy.crawler import CrawlerProcess
from stromtarife.items import StromtarifeItem

from scrapy.http import FormRequest

class VattenfallEasy24KemptenV1500Spider(scrapy.Spider):
    name = 'vattenfall-easy24-v1500-p87435'

    def start_requests(self):
        return [
            FormRequest(
                "https://www.vattenfall.de/de/stromtarife.htm",
                formdata={"place": "87435", "zipCode": "87435", "cityName": "Kempten",
                      "electricity_consumptionprivate": "1500", "street": "", "hno": ""},
            callback=self.parse
        ),
    ]

    def parse(self, response):
        item = StromtarifeItem()
        item['jahrespreis'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[3]/td[2]/text()').extract_first()
        item['treuebonus'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[2]/td/strong/text()').extract_first()
        item['sofortbonus'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[1]/td/strong/text()').extract_first()
        item['tarif'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[1]/h2/span/text()').extract_first()
        yield item



class VattenfallEasy24KemptenV2500Spider(scrapy.Spider):
    name = 'vattenfall-easy24-v2500-p87435'

    def start_requests(self):
        return [
                    FormRequest(
                    "https://www.vattenfall.de/de/stromtarife.htm",
                    formdata={"place": "87435", "zipCode": "87435", "cityName": "Kempten",
                              "electricity_consumptionprivate": "2500", "street": "", "hno": ""},
                    callback=self.parse
                ),
    ]

    def parse(self, response):
        item = StromtarifeItem()
        item['jahrespreis'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[3]/td[2]/text()').extract_first()
        item['treuebonus'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[2]/td/strong/text()').extract_first()
        item['sofortbonus'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[2]/form[1]/div/div[2]/table/tbody/tr[1]/td/strong/text()').extract_first()
        item['tarif'] = response.xpath('/html/body/main/div[1]/div[2]/div/div[3]/div[2]/div/div[1]/h2/span/text()').extract_first()
        yield item



process = CrawlerProcess()
process.crawl(VattenfallEasy24KemptenV1500Spider)
process.crawl(VattenfallEasy24KemptenV2500Spider)
process.start()

管道.py

import pymysql
from stromtarife.items import StromtarifeItem


class StromtarifePipeline(object):
    def __init__(self):
        self.connection = pymysql.connect("localhost","root","**********","stromtarife")
        self.cursor = self.connection.cursor()


    def process_item(self, item, spider):
        self.cursor.execute("INSERT INTO vattenfall (tarif, sofortbonus, treuebonus, jahrespreis) VALUES (%s, %s, %s, %s)", (item['tarif'], item['sofortbonus'], item['treuebonus'], item['jahrespreis']))
        self.connection.commit()
        self.cursor.close()
        self.connection.close()

settings.py(我只更改了那一行)

ITEM_PIPELINES = {
   'stromtarife.pipelines.StromtarifePipeline': 300,
}

那么我的代码有什么问题?我无法弄清楚,如果有人看到我丢失的东西,我会很高兴。提前致谢!

【问题讨论】:

    标签: mysql web-scraping scrapy pymysql scrapy-pipeline


    【解决方案1】:

    您不应该在每次处理项目时关闭您的 pymsql 连接。

    您应该像这样在管道中编写close_spider 函数,这样连接就在执行结束时关闭一次:

     def close_spider(self, spider):
            self.cursor.close()
            self.connection.close()
    

    此外,您需要在process_item结尾处退回您的物品

    您的文件 pipeline.py 应如下所示:

    import pymysql
    from stromtarife.items import StromtarifeItem
    
    
    class StromtarifePipeline(object):
        def __init__(self):
            self.connection = pymysql.connect("localhost","root","**********","stromtarife")
            self.cursor = self.connection.cursor()
    
    
        def process_item(self, item, spider):
            self.cursor.execute("INSERT INTO vattenfall (tarif, sofortbonus, treuebonus, jahrespreis) VALUES (%s, %s, %s, %s)", (item['tarif'], item['sofortbonus'], item['treuebonus'], item['jahrespreis']))
            self.connection.commit()
            return item
    
        def close_spider(self, spider):
            self.cursor.close()
            self.connection.close()
    

    更新:

    我试过你的代码,问题出在管道上,有两个问题:

    • 你试图索引欧元符号,我认为mysql不喜欢它。
    • 您的查询字符串构建不完善。

    我设法通过像这样编写 管道 来完成任务:

    def process_item(self, item, spider):
        query = """INSERT INTO vattenfall (tarif, sofortbonus, treuebonus, jahrespreis) VALUES (%s, %s, %s, %s)""" % ("1", "2", "3", "4")
        self.cursor.execute(query)
        self.connection.commit()
        return item
    

    我认为您应该从您尝试插入的价格中删除

    希望这有帮助,请告诉我。

    【讨论】:

    • 感谢您的回答。我更改了 process_item() 并添加了 close_spider(),但我的数据库中仍然没有任何内容。如果我得到结果,我可以进行下一步并遵循 rrschmidt 的建议。我真的无法弄清楚我的代码有什么问题..
    • 我在 cursor.execute() 函数中的 process_item() 中替换了该部分:...%s,%s)", (item['tarif'], item['sofortbonus'] , item['treuebonus'], item['jahrespreis'])) 带字符串:..%s, %s)", ("hey", "how", "are", "you"))不工作..
    • 我更新了之前的答案,如果这对你有用,请告诉我
    • 只有在使用noCursorTimeout()后才需要关闭cursor????
    【解决方案2】:

    除了您的 SQL 管道在编写第一项后关闭 SQL 连接这一事实(正如 Adrien 指出的那样)之外,您的爬虫还有另一个问题。

    另一个问题是:您的抓取工具只抓取每个结果页面的一项(并且也只访问一个结果页面)。我检查了 Vattenfall,通常会显示多个结果,我猜你想把它们全部刮掉。

    意味着您还必须迭代页面上的结果并在此过程中创建多个项目。这里的scrapy教程很好地解释了如何做到这一点:https://doc.scrapy.org/en/latest/intro/tutorial.html#extracting-quotes-and-authors

    【讨论】:

      【解决方案3】:

      首先,在代码开始 print(data) 必须在 db.commit() 之后,否则刚刚插入的数据数据库不会显示在print

      最后,从您的列名来看,如果上述想法不起作用,则可能是编码问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多