使用BeautifulSoup从investing.com上获取有关BTC / ETH的数据


问题内容

我已经编写了一些代码以从investing.com抓取BTC /
ETH时间序列,并且工作正常。但是,我需要更改请求调用,以使下载的数据来自Kraken而不是bitfinex默认值,而不是从01/06/2016而不是默认的开始时间。可以在网页上手动设置此选项,但是我不知道如何通过请求调用发送该选项,除了它可能涉及使用“
data”参数。感谢您的任何建议。

谢谢,

KM

已经用python编写的代码,可以很好地使用默认值

import requests
from bs4 import BeautifulSoup
import os
import numpy as np

# BTC scrape https://www.investing.com/crypto/bitcoin/btc-usd-historical-data
# ETH scrape https://www.investing.com/crypto/ethereum/eth-usd-historical-data

ticker_list = [x.strip() for x in open("F:\\System\\PVWAVE\\Crypto\\tickers.txt", "r").readlines()]
urlheader = {
  "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
  "X-Requested-With": "XMLHttpRequest"
}

print("Number of tickers: ", len(ticker_list))

for ticker in ticker_list:
    print(ticker)
    url = "https://www.investing.com/crypto/"+ticker+"-historical-data"
    req = requests.get(url, headers=urlheader, data=payload)
    soup = BeautifulSoup(req.content, "lxml")

    table = soup.find('table', id="curr_table")
    split_rows = table.find_all("tr")

    newticker=ticker.replace('/','\\')

    output_filename = "F:\\System\\PVWAVE\\Crypto\\{0}.csv".format(newticker)
    os.makedirs(os.path.dirname(output_filename), exist_ok=True)
    output_file = open(output_filename, 'w')
    header_list = split_rows[0:1]
    split_rows_rev = split_rows[:0:-1]

    for row in header_list:
        columns = list(row.stripped_strings)
        columns = [column.replace(',','') for column in columns]
        if len(columns) == 7:
            output_file.write("{0}, {1}, {2}, {3}, {4}, {5}, {6} \n".format(columns[0], columns[2], columns[3], columns[4], columns[1], columns[5], columns[6]))

    for row in split_rows_rev:
        columns = list(row.stripped_strings)
        columns = [column.replace(',','') for column in columns]
        if len(columns) == 7:
            output_file.write("{0}, {1}, {2}, {3}, {4}, {5}, {6} \n".format(columns[0], columns[2], columns[3], columns[4], columns[1], columns[5], columns[6]))

    output_file.close()

已下载数据以进行默认交换和默认日期范围,但我想指定Kraken以及默认的开始和结束时间(2016年6月1日以及最后一整天,即始终是昨天)


问题答案:

小背景

有很多网站根据用户活动(例如,您填写用户名和密码的登录页面)或单击按钮,使用称为 表单的形式 将数据发送到服务器。这样的事情正在发生。

我怎么知道的

  • 更改默认页面,然后转到 Kraken历史数据 页面。您将看到该URL已更改为https://www.investing.com/crypto/bitcoin/btc-usd-historical-data?cid=49799
  • 现在,右键单击该页面,然后单击 检查 。查看刚刚关闭的拆分屏幕的第一行。单击 网络 选项卡。此选项卡将显示您在浏览器中访问的任何网页的 请求/响应 周期。
  • 搜索您看到的红色按钮旁边的 清除 按钮,然后单击它。现在,您的工作情况一清二楚。在该页面上更改日期后,您将能够看到请求已发送到服务器。
  • 根据需要更改日期,然后单击“ 应用” 。您将看到名称为 HistoricalDataAjax 的请求已发送到服务器(请参阅下面的图片以更清楚)。单击它,然后在“ 标题” 选项卡中向下滚动。您可以看到名为 Form Data 的部分。这是发送到服务器的额外的隐藏(尚未隐藏)信息。由于您未在网址中看到任何更改,因此将其作为 POST 请求发送。
  • 您还可以在同一 标题 部分中看到 请求URLhttps://www.investing.com/instruments/HistoricalDataAjax

在“网络”标签下

现在做什么?

您需要很聪明,并在python代码中进行 3个 更改。

  • 将请求从 GET 更改为 POST
  • 发送 表单数据 作为该请求的有效内容。
  • 将网址更改为您刚刚在“ 标题” 标签中看到的网址。

url =“
https://www.investing.com/instruments/HistoricalDataAjax

有效载荷= {‘header’:’BTC / USD海妖历史数据’,’st_date’:‘12 / 01/2018’,’end_date’:‘12 /
01/2018’,’sort_col’:’date’,’action
‘:’historical_data’,’smlID’:‘145284’,’sort_ord’:’DESC’,’interval_sec’:’Daily’,’curr_id’:‘49799’}

request.post(URL,data = payload,headers = urlheader)

进行上述更改,并使代码的其他部分保持不变。您将得到想要的结果。您也可以根据需要修改日期。