【发布时间】: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