I am trying to get the text value inside a span tag having an id attribute using beautifulsoup. But it returs no text, rather only a '-'.
I have tried scraping using the div tag with the class attribute and then navigating to the span tag using findChildren() function too, but it still returns a "-". Here is the html that I am trying to scrape from the website https://etherscan.io/tokens-nft.
<div class="row align-items-center">
<div class="col-md-4 mb-1 mb-md-0">Transfers:</div>
<div class="col-md-8"></div>
<span id="totaltxns">266,765</span><hr class="hr-space">
</div>
And here is my python code:
from urllib2 import Request,urlopen
from bs4 import BeautifulSoup as soup
import array
url = 'https://etherscan.io/tokens-nft'
response = Request(url, headers = {'User-Agent':'Mozilla/5.0'})
page_html = urlopen(response).read()
page_soup = soup (page_html,'html.parser')
count = 0
total_nfts = 2691 #Hard-coded value
supply = []
totalAddr = []
transCount = []
row = []
print('All non-fungible tokens in order of Transfers')
for nfts in page_soup.find_all("a", class_ ='text-primary'):
link = nfts.get('href')
new_url = "https://etherscan.io/"+link
name = nfts.text
print('NFT '+name)
response2 = Request(new_url, headers = {'User-Agent':'Mozilla/5.0'})
phtml = urlopen(response2).read()
psoup = soup (phtml,'html.parser')
#Get tags
tags = []
#print('Tags')
for allTags in psoup.find_all("a",class_ = 'u-label u-label--xs u-label--secondary'):
tags.append(allTags.text.encode("ascii"))
count+=1
if(len(tags)!=0):
print(tags)
#Get total supply
ts = psoup.find("span", class_ = "hash-tag text-truncate")
ts = ts.text
#print(ts)
#Get holders
holders = psoup.find("div", {"id":"ContentPlaceHolder1_tr_tokenHolders"})
holders = holders.findChildren()[1].findChildren()[1].text
#print(holders)
#Get transfers/transactions
print(psoup.find("span", attrs={"id":"totaltxns"}).text)
print('Total number of NFTS '+str(count))
I have also tried:
transfers = psoup.find("span", attrs={"id":"totaltxns"})
but that doesn't work either.
The correct parsing should return 266,765.