在 SQL 中显示多行项目
Display more than one row of items in SQL
我正在使用 Scrapy 制作一个收集汇率数据的网络抓取工具,并希望使用 mysql 在 table 中显示数据,但我的代码只显示第一个一组数据,用于货币和汇率,我不确定如何让它显示所有数据。这是我的代码:
蜘蛛代码:
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()
在您当前使用的逻辑中,您只有 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()
我正在使用 Scrapy 制作一个收集汇率数据的网络抓取工具,并希望使用 mysql 在 table 中显示数据,但我的代码只显示第一个一组数据,用于货币和汇率,我不确定如何让它显示所有数据。这是我的代码:
蜘蛛代码:
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()
在您当前使用的逻辑中,您只有 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()