Home > Software design >  A working snippet can no longer grab the data it used to
A working snippet can no longer grab the data it used to

Time:09-21

I am trying to figure out why the part of the snippet get_transfer_count does not work. I used this code in the past and has been fine until lately.

import requests
from bs4 import BeautifulSoup

header = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0",
}
tokenurl = (
    "https://bscscan.com/token/0xe56842ed550ff2794f010738554db45e60730371"
)
token = "0xe56842ed550ff2794f010738554db45e60730371"

contractpage = requests.get(tokenurl, headers=header)
ca = BeautifulSoup(contractpage.content, "html.parser")
name = ca.h1.span.get_text(strip=True)
price = ca.select_one(".card-body .d-block").get_text(strip=True)
cap = ca.select_one("#pricebutton").get_text(strip=True)

def get_transfer_count(str:token)->str:
    with requests.Session() as s:
        s.headers = {'User-Agent':'Mozilla/5.0'}
        r = s.get(f'https://bscscan.com/token/{token}') 
        try:   
            sid = re.search(r"var sid = '(.*?)'", r.text).group(1)
            r = s.get(f'https://bscscan.com/token/generic-tokentxns2?m=normal&contractAddress={token}&a=&sid={sid}&p=1')
            return re.search(r"var totaltxns = '(.*?)'", r.text).group(1)
        except:
            pass
transcount = get_transfer_count(token)

print("Token:", name)
print("PRICE:", price)
print("Fully Diluted Market Cap:", cap)
print ("Transfer: ", transcount)
print()

Previous Output:

Token: Binemon
PRICE: $0.02@ 0.000040 BNB(-17.38%)
Fully Diluted Market Cap: $15,003,573.00
Transfer:  440,283

Exptected Output:

Token: Binemon
PRICE: $0.02@ 0.000040 BNB(-17.38%)
Fully Diluted Market Cap: $15,003,573.00
Transfer:  440,283   #-- this part no longer works

CodePudding user response:

You are missing an import:

import re

You don't get an Exception because you are using a bare except:

except:
    pass

This is not recommended, because it causes errors like this.

  • Related