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.
JavaScript
x
20
20
1
from bs4 import BeautifulSoup
2
from urllib import request
3
from urllib.request import Request, urlopen
4
5
url = "https://bscscan.com/block/9478762"
6
headers = {"User-Agent": "Mozilla/5.0"}
7
8
req = Request(url, headers=headers)
9
html = urlopen(req).read()
10
soup = BeautifulSoup(html, "html.parser")
11
12
blockheight = soup.find('div', attrs={'class': 'font-weight-sm-bold mr-2'})
13
print ("Block Height: ", blockheight)
14
15
blocktimestamp = soup.find('div', attrs={'class': 'far fa-clock small mr-1'})
16
print ("Timestamp ",blocktimestamp)
17
18
blocktransactions = soup.find('div', class_ = 'u-label u-label--value u-label--primary rounded my-1')
19
print ("Transactions ", blocktransactions)
20
Current Output:
JavaScript
1
4
1
Block Height: None
2
Timestamp: None
3
Transactions: None
4
Wanted Output:
JavaScript
1
6
1
Block Height: 9478762
2
Timestamp: Jul-25-2021 11:43:52 PM +UTC
3
Transactions: 223 -> transactions https://bscscan.com/txs?block=9478762
4
37 -> contract internal transactions https://bscscan.com/txsInternal?block=9478762
5
Validated by: 0xb218c5d6af1f979ac42bc68d98a5a0d796c6ab01
6
Advertisement
Answer
I hope this helps:
JavaScript
1
20
20
1
from bs4 import BeautifulSoup
2
from urllib import request
3
from urllib.request import Request, urlopen
4
5
url = "https://bscscan.com/block/9478762"
6
headers = {"User-Agent": "Mozilla/5.0"}
7
8
req = Request(url, headers=headers)
9
html = urlopen(req).read()
10
soup = BeautifulSoup(html, "html.parser")
11
12
blockheight = soup.find('span', attrs={'class': 'font-weight-sm-bold mr-2'}).contents[0]
13
print ("Block Height: ", str(blockheight).replace("n", ""))
14
15
blocktimestamp = soup.find('i', attrs={'class': 'far fa-clock small mr-1'}).next_sibling
16
print ("Timestamp: ",str(blocktimestamp).replace("n", ""))
17
18
blocktransactions = soup.find('a', class_ = 'u-label u-label--value u-label--primary rounded my-1').contents[0]
19
print ("Transactions: ", blocktransactions)
20
Output:
JavaScript
1
4
1
Block Height: 9478762
2
Timestamp: 2 hrs 35 mins ago (Jul-25-2021 11:43:52 PM +UTC)
3
Transactions: 223 transactions
4