json - 从whattomine中的链接接收JSON数据而不抓取HTML

标签 json python-2.7 pandas python-requests urllib2

说明

This link是输入硬件统计信息(算力、功率、电力成本等)后您将前往的位置。顶部栏(蓝色 Twitter 关注按钮下方)是一个指向 JSON 文件的链接,该文件是在页面加载后创建的,其中包含输入的硬件统计信息;单击该 JSON 链接会将您重定向到另一个 URL ( https://whattomine.com/asic.json )。

目标

我的目标是在通过终端操作 URL 字符串中的值后直接访问该 JSON 文件。例如,如果我想将 URL 这部分的哈希率从 100 更改为 150:

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

在 URL 操作(如上所述,但不限于)之后,我希望接收 JSON 输出,以便我可以挑选出所需的数据。

我的代码

咨询: 我从 2017 年 6 月开始学习 Python 编程,请原谅。

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)'),))

感谢您的帮助和时间!

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

最佳答案

这只是一条评论。以下方法就足够了(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 中就足够了。 您可以像在 ? 符号

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

关于json - 从whattomine中的链接接收JSON数据而不抓取HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47258425/

相关文章:

python - 将函数编码为 JSON

pandas - 被 pandas DataFrame memory_usage 和副本搞糊涂了

java - 如何构建 REST dto?

javascript - 通过数组按给定顺序对 JSON 进行排序(JavaScript、D3)

python - 我在使用 python 将 JSON 转换为 CSV 时遇到问题

python - json.dump() 是否附加到文件?

python - Python Tkinter程序崩溃时忘记了()

python - 有效比较 Pandas Dataframe 中的行数据

python - 在 panda 数据框中添加具有重复索引的行

python - 如何在 Pandas 数据框中对最大和最小时间戳进行分组