Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用MySQL在表中显示长货币名称_Python_Mysql_Scrapy_Scrapy Pipeline - Fatal编程技术网

Python 使用MySQL在表中显示长货币名称

Python 使用MySQL在表中显示长货币名称,python,mysql,scrapy,scrapy-pipeline,Python,Mysql,Scrapy,Scrapy Pipeline,我正在使用Scrapy制作一个网络刮板,它可以刮取与欧元相比的货币兑换率,并希望在MySQL表中显示汇率、货币名称和名称的缩写版本。我已经能够将汇率和缩写名称放在表中,但是当我尝试使用完整的货币名称时,唯一放在表中的是第一个结果。这是我的密码: 刮板本身: import scrapy from ..items import EurotocurrencyItem class CurrencySpider(scrapy.Spider): name = 'currency' star

我正在使用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):
        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()
            currencyl = response.xpath('//td[@class="alignLeft"]//text()').extract_first()
            rate = exchange_rate.css('.rate::text').extract_first()

            item['currency'] = currency
            item['currencyl'] = currencyl
            item['rate'] = rate

            yield item
items.py:

import scrapy


class EurotocurrencyItem(scrapy.Item):
    currency = scrapy.Field()
    rate = scrapy.Field()
    currencyl = scrapy.Field()
pipelines.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='notrealpassword',
            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,
                    currencyl 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, %s  )""", (
            item['currency'],
            item['currencyl'],
            item['rate']
        ))
        self.conn.commit()

您的
货币选择中有两个小错误:
首先,您必须迭代
汇率
,而不是
响应
(正如您对货币和汇率所做的那样)。
此外,您的
xpath()
中缺少一个

因此,这应该如预期的那样起作用:

currencyl=exchange_-rate.xpath('.//td[@class=“alignLeft”]//text()).extract_-first()

您的
货币选择中有两个小错误:
首先,您必须迭代
汇率
,而不是
响应
(正如您对货币和汇率所做的那样)。
此外,您的
xpath()
中缺少一个

因此,这应该如预期的那样起作用:

currencyl=exchange_-rate.xpath('.//td[@class=“alignLeft”]//text()).extract_-first()