【问题标题】:python data scraping change currencypython数据抓取改变货币
【发布时间】:2018-07-27 00:27:18
【问题描述】:

我正在尝试使用页面 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 请求欧元货币网页?

【问题讨论】:

    标签: python web-scraping beautifulsoup screen-scraping


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 感谢您的意见!我完全是 html 的菜鸟。页面是否使用 usd_per_eur = float(soup.find("div",{"id":"currency-exchange-rates"})['data-eur']) 获得今天的转化率,然后将所有历史美元值相乘以今天的汇率?还是 cmc 在其数据库中有以欧元表示的数据并根据用户选择的货币标志加载它?
    • 是的,有一个 ID 为 currency-exchange-rates 的 div 包含汇率。尚未在 JS 中四处寻找,但该页面似乎正在使用该预加载的汇率数据在用户选择不同的货币时立即重新计算。当您更改货币时,没有任何请求返回服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多