【问题标题】:Display more than one row of items in SQL在 SQL 中显示多行项目
【发布时间】:2020-08-17 13:20:59
【问题描述】:

我正在使用 Scrapy 制作一个网络爬虫,它收集汇率数据,并希望使用 mysql 在表格中显示数据,但使用我的代码它只显示第一组数据,用于货币和汇率,我不确定如何让它显示所有数据。这是我的代码:

蜘蛛码:

import scrapy
from ..items import EurotocurrencyItem

class CurrencySpider(scrapy.Spider):
    name = 'currency'
    start_urls = [
        'https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html'
    ]

    def parse(self, response):

        items = EurotocurrencyItem()

        currency = response.xpath('//td[@class="currency"]//text()').extract()
        rate = response.css('.rate::text').extract()

        items['currency'] = currency
        items['rate'] = rate
        yield items

Piplines.py 代码:

import mysql.connector


class EurotocurrencyPipeline:

    def __init__(self):
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = mysql.connector.connect(
            host='localhost',
            user='root',
            passwd='notactualpassword',
            database='currency'
        )
        self.curr = self.conn.cursor()

    def create_table(self):
        self.curr.execute("""DROP TABLE IF EXISTS currency_tb""")
        self.curr.execute("""create table currency_tb(
                    currency text,
                    rate text
                    )""")

    def process_item(self, item, spider):
        self.store_db(item)
        return item

    def store_db(self, item):
        self.curr.execute("""insert into currency_tb values(%s, %s  )""", (
            item['currency'][0],
            item['rate'][0],
        ))
        self.conn.commit()

【问题讨论】:

    标签: python mysql scrapy scrapy-pipeline


    【解决方案1】:

    在您当前使用的逻辑中,您只有 1 个项目,看起来像这样:

    item = {'currency': ["USD", "JPY", ...],
            'rate': ["1.0876", "115.87", ...]}
    

    在您的 store_db 方法中,您只需将每个列表的第一个元素插入 mysql。您应该重写 parse 中的逻辑,以便每个汇率产生 1 个项目:

    exchange_rates = response.xpath('//*[@class="forextable"]//tr')
    for exchange_rate in exchange_rates:
        item = EurotocurrencyItem()
        currency = exchange_rate.xpath('.//td[@class="currency"]//text()').extract_first()
        rate = exchange_rate.css('.rate::text').extract_first()
        item['currency'] = currency
        item['rate'] = rate
        yield item
    

    如果您随后按如下方式更新您的 store_db 方法,那应该是不错的选择:

    def store_db(self, item):
            self.curr.execute("""insert into currency_tb values(%s, %s  )""", (
                item['currency'],
                item['rate'],
            ))
            self.conn.commit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2013-03-09
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多