The snippet below does not really display the intended data as it returns None. Any idea and inputs as how to do it properly will be very helpful.
from bs4 import BeautifulSoup from urllib import request from urllib.request import Request, urlopen url = "https://bscscan.com/block/9478762" headers = {"User-Agent": "Mozilla/5.0"} req = Request(url, headers=headers) html = urlopen(req).read() soup = BeautifulSoup(html, "html.parser") blockheight = soup.find('div', attrs={'class': 'font-weight-sm-bold mr-2'}) print ("Block Height: ", blockheight) blocktimestamp = soup.find('div', attrs={'class': 'far fa-clock small mr-1'}) print ("Timestamp ",blocktimestamp) blocktransactions = soup.find('div', class_ = 'u-label u-label--value u-label--primary rounded my-1') print ("Transactions ", blocktransactions)
Current Output:
Block Height: None Timestamp: None Transactions: None
Wanted Output:
Block Height: 9478762 Timestamp: Jul-25-2021 11:43:52 PM +UTC Transactions: 223 -> transactions https://bscscan.com/txs?block=9478762 37 -> contract internal transactions https://bscscan.com/txsInternal?block=9478762 Validated by: 0xb218c5d6af1f979ac42bc68d98a5a0d796c6ab01
Advertisement
Answer
I hope this helps:
from bs4 import BeautifulSoup from urllib import request from urllib.request import Request, urlopen url = "https://bscscan.com/block/9478762" headers = {"User-Agent": "Mozilla/5.0"} req = Request(url, headers=headers) html = urlopen(req).read() soup = BeautifulSoup(html, "html.parser") blockheight = soup.find('span', attrs={'class': 'font-weight-sm-bold mr-2'}).contents[0] print ("Block Height: ", str(blockheight).replace("n", "")) blocktimestamp = soup.find('i', attrs={'class': 'far fa-clock small mr-1'}).next_sibling print ("Timestamp: ",str(blocktimestamp).replace("n", "")) blocktransactions = soup.find('a', class_ = 'u-label u-label--value u-label--primary rounded my-1').contents[0] print ("Transactions: ", blocktransactions)
Output:
Block Height: 9478762 Timestamp: 2 hrs 35 mins ago (Jul-25-2021 11:43:52 PM +UTC) Transactions: 223 transactions