saga
saga

Reputation: 755

How do I grab the table content from a webpage with javascript using python?

I like to grab the table content from this page. The following is my code and I got NaN (without the data). How come the numbers are not showing up? How do I grab the table with the corresponding data? Thanks.

enter image description here

Upvotes: 0

Views: 137

Answers (1)

chitown88
chitown88

Reputation: 28640

You can get a nice json format from the api:

import requests
import pandas as pd

url = 'https://api.blockchain.info/stats'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
params = {'cors': 'true'}

data = requests.get(url, headers=headers, params=params).json()

# if you want it as a table
df = pd.DataFrame(data.items())

Option 2:

Let the page fully render. There is abetter way to use wait with Selenium, but just quickly threw a 5 second wait in there to show:

from selenium import webdriver
import pandas as pd
import time

url = 'https://www.blockchain.com/stats'


browser = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
browser.get(url)
time.sleep(5)

dfs = pd.read_html(browser.page_source)
print(dfs[0])

browser.close()

Output:

                    0                   1                   2   3
0         Blocks Mined                 150                 150 NaN
1  Time Between Blocks        9.05 minutes        9.05 minutes NaN
2       Bitcoins Mined  1,875.00000000 BTC  1,875.00000000 BTC NaN

Upvotes: 1

Related Questions