gpt4 book ai didi

python数据抓取改变货币

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:50 25 4
gpt4 key购买 nike

我正在尝试使用页面 https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20180216 中的以下脚本从 coinmarketcap.com 获取比特币和以太坊历史价格:

import requests
import xlwt
import traceback
import pandas as pd
import urllib.request as req
from bs4 import BeautifulSoup
from pandas import ExcelWriter

def process_data(data, coin, workbook):
try:
sheet = workbook.add_sheet(coin)
soup = BeautifulSoup(data, 'lxml')
table = soup.find_all('table')[0]

df = pd.read_html(str(table))
k = (df[0].to_json(orient='records'))
import json
resp = json.loads(k)
# resp format: {'Date': 'Apr 28, 2013', 'Open': 135.3, 'High': 135.98, 'Low': 132.1, 'Close': 134.21, 'Volume': '-', 'Market Cap': 1500520000}
lst = [[each['Date'], each['Open'], each['High'], each['Low'], each['Close'], each['Volume'], each['Market Cap']] for each in resp]

for i, l in enumerate(lst):
for j, col in enumerate(l):
sheet.write(i, j, col)
except Exception as e:
print (e)
print(traceback.print_exc())

coins = ['Bitcoin', 'Ethereum']

workbook = xlwt.Workbook(encoding='ascii')

if __name__ == '__main__':
for each in coins:
coin = each.lower()
url = "https://coinmarketcap.com/currencies/"+ coin + "/historical-data/?start=20090428&end=20180207"
print (url)
try:
a = requests.get(url)
process_data(a.text, each, workbook)
except Exception as e:
print 'error in fetching data', coin
workbook.save('cmc_data_f.xls')

脚本从页面获取 html 响应并写入 Excel 文件。问题是网页默认返回美元数据。我想要欧元数据。

网站上有一个下拉菜单可供选择多种货币。但是当我从 python 发送请求时,它默认返回美元。

有谁知道是否有办法从 coinmarketcap.com 请求欧元货币网页?

最佳答案

不,但您可以从页面中提取所需的数据,以匹配页面正在进行的转换。

table = ...位之后,添加此行以获取汇率:

usd_per_eur = float(soup.find("div",{"id":"currency-exchange-rates"})['data-eur'])

然后调整循环代码,如下所示:

for i, l in enumerate(lst):
for j, col in enumerate(l):
if 0 < j < 5: # indices 1-4 of response contain USD values
try:
converted = float(col)/usd_per_eur
except ValueError:
pass
else:
col = str(round(converted,2))
sheet.write(i, j, col)

关于python数据抓取改变货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48820947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com