【问题标题】:Recieve JSON data from link in whattomine without scraping HTML从whattomine中的链接接收JSON数据而不抓取HTML
【发布时间】:2018-04-25 18:52:55
【问题描述】:

说明

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

感谢您的帮助和时间!

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

【问题讨论】:

  • 虽然对某些人来说它看起来像是pyOpenSSL version issue,但卸载并重新安装已经解决了这个问题。 SO中另一个较旧的答案要求do the following
  • 非常喜欢@SudheeshSinganamalla。如果您想将其发布为答案,我将非常乐意支持它。您更新 pyOpenSSL 的建议对我有用,我现在收到 &lt;Response [200]&gt; 谢谢!
  • 还没有高兴@garej,我似乎仍然无法超越默认的 JSON 数据;如中,URL 操作未在 JSON 结果中注册。答案帮助我更进一步。我仍然摸不着头脑,希望有人可以帮助我更接近一步,或者为我提供一些时间和经验来完成这额外的一步。

标签: json python-2.7 pandas python-requests urllib2


【解决方案1】:

看起来其他一些人也遇到了类似的问题。

虽然对于某些人来说它似乎像pyOpenSSL version issue,但卸载并重新安装已解决了问题。 SO中另一个较旧的答案要求do the following

【讨论】:

    【解决方案2】:

    这只是一个评论。以下方法就足够了(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中就足够了。 您可以像在 ? 符号后的查询部分中那样添加所有参数

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2020-06-30
      • 2013-06-18
      • 2021-01-08
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多