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

时间:2018-12-21 20:31:31

标签: python beautifulsoup

我已经编写了一些代码,以便从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日以及最后一整天,即始终是昨天)

1 个答案:

答案 0 :(得分:2)

背景很少

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

我怎么知道的?

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

Under Networks tab view

现在该怎么办?

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

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

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

    payload = {'header':'BTC / USD Kraken历史数据','st_date':'12 / 01/2018','end_date':'12 / 01/2018','sort_col':'date' ,“操作”:“ historical_data”,“ smlID”:“ 145284”,“ sort_ord”:“ DESC”,“ interval_sec”:“每日”,“ curr_id”:“ 49799”}

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

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