【问题标题】:Scrape html data with beautifulsoup 4用 beautifulsoup 4 抓取 html 数据
【发布时间】:2017-08-31 02:41:24
【问题描述】:

我正在尝试从 nanopool 中抓取数据以在我的 python 脚本中使用,但每当我尝试抓取数据时,我只会得到类,而不是我正在寻找的文本。有问题的具体网页的链接可以找到here
非常简化的页面源码

我要获取的第一条数据:

<table class="table table-hover table-bordered" style="margin-bottom: 5px;" data-bind="with: calc">
<tbody>
        <tr data-bind="with: hour"><td>Hour</td>
            <td class="text-right" data-bind="text: coins">0.00015</td>
            <td class="text-right" data-bind="text: bitcoins">0.000012</td>
            <td class="text-right" data-bind="text: dollars">0.058</td><td class="text-right" data-bind="text: euros">0.048</td>
            <td class="text-right" data-bind="text: yuan">0.372</td><td class="text-right" data-bind="text: rubles">3.244</td>
        </tr>
</tbody>

我要获取的第二条数据:
&lt;div class="panel-body" data-bind="text: balance() + ' ETH'"&gt;0.02743061 ETH&lt;/div&gt;

我一直在尝试使用的代码:
输入

r = requests.get('https://eth.nanopool.org/account/0x2a156c6dd3bdf2a0c5b284b45b2396c053c2a63d')
soup = BeautifulSoup(r.text, 'html.parser')
balance = soup.findAll('div', attrs = {'class': 'panel-body'})[3]
calc_box = soup.findAll('table')[3].findAll('tr')[2].find('td', {'class', 'text-right'})
print(calc_box)
print(balance)

输出

<td class="text-right" data-bind="text: coins"></td>
<div class="panel-body" data-bind="text: balance() + ' ETH'"></div>

【问题讨论】:

  • 打印r.text,你会看到table[3]没有文字。它可能会动态加载这些值,您将在 SO 上看到解决该问题的其他答案。

标签: python html css python-3.x beautifulsoup


【解决方案1】:

它异步获取数据。

余额 API:

https://eth.nanopool.org/api/v1/balance_hashrate/0x2a156c6dd3bdf2a0c5b284b45b2396c053c2a63d

按过去 6 小时的平均算力计算的近似日收入。

过去 6 小时 API 的平均算力:

https://eth.nanopool.org/api/v1/avghashrate/0x2a156c6dd3bdf2a0c5b284b45b2396c053c2a63d

获得最近 6 小时的平均算力后:

{"status":true,"data":{"h1":9.916666666666668,"h3":19.833333333333336,"h6":20.77777777777778,"h12":22.54861111111111,"h24":23.67013888888889}}

使用 'h6' 值 (20.7),将 require 发送到 Calculator API:

https://eth.nanopool.org/api/v1/approximated_earnings/20.7

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多