【问题标题】:Scraping HTML table using BS4使用 BS4 抓取 HTML 表格
【发布时间】:2020-12-30 19:22:08
【问题描述】:

我一直在尝试从网址https://www.binance.com/en/futures/funding-history/0 中提取表格数据。

以下代码仅提取表格的一部分(仅标题)。

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.binance.com/en/futures/funding-history/0')
soup = BeautifulSoup(r.text, 'html.parser')
resultsTable = soup.findChildren('table')[0]
rows = resultsTable.findChildren(['tr'])
print(rows)

我有什么遗漏的或者有更好的想法来完成这项工作吗?

【问题讨论】:

  • 你缺少的是表格是由JS动态加载的,所以BeautifulSoup不会看到它。

标签: python-3.x web-scraping beautifulsoup


【解决方案1】:

但是,您可以做的是使用 API 并获取表格数据。

方法如下:

import pandas as pd
import requests
from tabulate import tabulate

response = requests.get("https://www.binance.com/fapi/v1/premiumIndex").json()
print(tabulate(pd.DataFrame(response), headers="keys"))

输出:

    symbol          markPrice      indexPrice    estimatedSettlePrice    lastFundingRate    interestRate    nextFundingTime           time
--  ---------  --------------  --------------  ----------------------  -----------------  --------------  -----------------  -------------
 0  EOSUSDT        2.595           2.59304                 2.59623            0.00010306          0.0001      1609344000000  1609326650000
 1  SUSHIUSDT      2.43947         2.4375                  2.44375            0.00014561          0.0001      1609344000000  1609326650000
 2  CVCUSDT        0.08707         0.0870002               0.0880487          0.00010314          0.0001      1609344000000  1609326650000
 3  BALUSDT       13.5316         13.564                  13.6151            -0.00015685          0.0001      1609344000000  1609326650000
 4  BNTUSDT        1.31368         1.31368                 1.31514            0.0001              0.0001      1609344000000  1609326650000
 5  KNCUSDT        0.819237        0.817172                0.818279           0.00070895          0.0001      1609344000000  1609326650000
 6  SRMUSDT        1.0391          1.03867                 1.04142            0.00013314          0.0001      1609344000000  1609326650000
 7  ENJUSDT        0.13201         0.131963                0.132194           0.00045462          0.0001      1609344000000  1609326650000
 8  ZRXUSDT        0.3544          0.354151                0.355443           0.00011162          0.0001      1609344000000  1609326650000
 9  QTUMUSDT       2.2153          2.21517                 2.21938            0.0001              0.0001      1609344000000  1609326650000
10  ZENUSDT       11.0735         11.0484                 11.0732             0.0001              0.0001      1609344000000  1609326650000
11  ATOMUSDT       5.66482         5.65481                 5.67682            0.00033165          0.0001      1609344000000  1609326650000
12  IOTAUSDT       0.293784        0.293287                0.294668           0.00020508          0.0001      1609344000000  1609326650000
13  WAVESUSDT      6.65303         6.65848                 6.68021           -0.00046047          0.0001      1609344000000  1609326650000
14  ADAUSDT        0.179745        0.179608                0.18002            0.0001              0.0001      1609344000000  1609326650000
15  NEARUSDT       1.2445          1.24402                 1.24569            0.0001              0.0001      1609344000000  1609326650000

and so on ...

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2013-12-04
    • 1970-01-01
    • 2019-01-15
    相关资源
    最近更新 更多