psuresh
psuresh

Reputation: 584

Scraping HTML table using BS4

I have been trying to extract the table data from web url https://www.binance.com/en/futures/funding-history/0.

The code below extracts only a portion(headers only) of the table.

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)

Is there anything I have been missing or is there a better idea to do this job?

Upvotes: 0

Views: 76

Answers (1)

baduker
baduker

Reputation: 20042

But, what you could do is use the API and get the tabular data.

Here's how:

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"))

Output:

    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 ...

Upvotes: 1

Related Questions