在 whattomine 中接收来自 link 的 JSON 数据而不抓取 HTML

Recieve JSON data from link in whattomine without scraping HTML

说明

This link is where you are sent to after entering in your hardware stats (hashrate, power, power cost, etc.). On the top bar (below the blue Twitter follow button) is a link to a JSON file created after the page loads with the hardware stats information entered; clicking on that JSON link redirects you to another URL (https://whattomine.com/asic.json).

目标

我的目标是在通过终端操作 URL 字符串中的值后直接访问 JSON 文件。例如,如果我想在 URL:

的这一部分将哈希率从 100 更改为 150

[sha256_hr]=100& ---> [sha256_hr]=150&

经过URL操作(如上,但不限于),我想接收JSON输出,以便我可以挑选出所需的数据。

我的代码

咨询:我开始Python编程~2017年6月,请见谅

import json
import pandas as pd
import urllib2
import requests


hashrate_ghs = float(raw_input('Hash Rate (TH/s): '))
power_W = float(raw_input('Power of Miner (W): '))
electric_cost = float(raw_input('Cost of Power ($/kWh): '))
hashrate_ths = hashrate_ghs * 1000

initial_request = ('https://whattomine.com/asic?utf8=%E2%9C%93&sha256f=true&factor[sha256_hr]={0}&factor[sha256_p]={1}&factor[cost]={2}&sort=Profitability24&volume=0&revenue=24h&factor[exchanges][]=&factor[exchanges][]=bittrex&dataset=Main&commit=Calculate'.format(hashrate_ths, power_W, electric_cost))
data_stream_mine = urllib2.Request(initial_request)

json_data = requests.get('https://whattomine.com/asic.json')
print json_data

我的代码出错

我收到 HTTPS 握手错误。这是我的 Python 新鲜度第二明显的地方:

Traceback (most recent call last):
  File "calc_1.py", line 16, in <module>
    s.get('https://whattomine.com/asic.json')
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='whattomine.com', port=443): Max retries exceeded with url: /asic.json (Caused by SSLError(SSLError(1, u'[SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)'),))

感谢您的帮助和时间!

如有任何更改或有关此问题的更多信息,请告知我。

看来其他几个人也遇到了类似的问题。

虽然对于某些人来说它看起来像 pyOpenSSL version issue, uninstalling and reinstalling which has fixed the problem. Another older answer in SO asks to do the following

这只是一条评论。以下方法就足够了 (Python 3).

import requests

initial_request = 'http://whattomine.com/asic.json?utf8=1&dataset=Main&commit=Calculate'

json_data = requests.get(initial_request)
print(json_data.json())

这部分的关键点 - 将 .json 放入您的 initial_request 就足够了。 您可以像在 ? 符号

之后的查询部分中那样添加所有参数