Issue
I grabbed few lines of data from a url. I am trying to compare one largest value of a type against another. Im missing something as I failed to extract it properly.
import requests, re, time
from bs4 import BeautifulSoup
from selenium import webdriver
trim = re.compile(r'[^\d,.]+')
driver = webdriver.Chrome('chromedriver.exe')
url = "https://poocoin.app/rugcheck/0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3/top-holders"
driver.get(url)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'lxml')
t = soup.find('table', class_='table table-bordered table-condensed text-small')
bigwallet = 0
bigcontract = 0
contractbalance = 0
walletbalance = 0
for row in soup.select("tr:has(td)")[:10]:
addr = row.find_all("td")[0].text[0:]
trans = row.find_all("td")[4].text[0:]
bal = row.find_all("td")[5].text[0:].strip()
tbal = trim.sub('', bal).replace(",", "")
tbal = float(tbal)
wtype = row.find_all("td")[2].text[0:].strip()
if (str(wtype) == "Contract"):
contractbalance = float(tbal)
else:
walletbalance = float(tbal)
if (walletbalance) > (contractbalance):
bigwallet = walletbalance
else:
bigcontract = contractbalance
bigwallet = bigwallet
bigcontract = bigcontract
print(" {} {:<20} {:<5} {:>5} ".format(addr, bal, trans, wtype))
print (" Largest Contract: {} Largest Wallet: {} ".format(bigwallet, bigcontract))
driver.quit()
Current Output: #-- current problematic output
0x9adc6fb78cefa07e13e9294f150c1e8c1dd566c0 12,704,309,869,844.8669 325911 Contract
0xc95063d946242f26074a76c8a2e94c9d735dfc78 7,745,539,348,064.8244 11 Wallet
0x0000000000000000000000000000000000000001 423,229,310,780,801.1327 159 Contract
0xff3dd404afba451328de089424c74685bf0a43c9 15,407,439,439,186.9579 389180 Contract
0x86b695aaa2600668cec754c7827357626b188054 10,311,345,756,789.1980 9 Wallet
0x010b86c90654905611b31dbfaf5883ba616b9833 0.0000 1 Wallet
#-- problematic part of my code
Largest Contract: 0 Largest Wallet: 15407439439186.957
Wanted Output:
Largest Contract: 423,229,310,780,801.1327 Largest Wallet: 10,311,345,756,789.1980
0x9adc6fb78cefa07e13e9294f150c1e8c1dd566c0 12,704,309,869,844.8669 325911 Contract
0xc95063d946242f26074a76c8a2e94c9d735dfc78 7,745,539,348,064.8244 11 Wallet
0x0000000000000000000000000000000000000001 423,229,310,780,801.1327 159 Contract
0xff3dd404afba451328de089424c74685bf0a43c9 15,407,439,439,186.9579 389180 Contract
0x86b695aaa2600668cec754c7827357626b188054 10,311,345,756,789.1980 9 Wallet
0x010b86c90654905611b31dbfaf5883ba616b9833 0.0000 1 Wallet
Solution
You can collect all those values to respective list and then take the max
value among those.
bigwallet = [] # List of wallet values
bigcontract = [] # List of contract values
for row in soup.select("tr:has(td)")[:10]:
addr = row.find_all("td")[0].text[0:]
trans = row.find_all("td")[4].text[0:]
bal = row.find_all("td")[5].text[0:].strip()
tbal1 = trim.sub('', bal).replace(",", "")
tbal = float(tbal1)
wtype = row.find_all("td")[2].text[0:].strip()
if (str(wtype) == "Contract"):
bigcontract.append(tbal) # Append to bigcontract if its a contact one else append to bigwallet.
else:
bigwallet.append(tbal)
#print(" {} {:<20} {:<5} {:>5} ".format(addr, bal, trans, wtype))
print (" Largest Contract: {} Largest Wallet: {} ".format(max(bigcontract),max(bigwallet))) # print max of bigcontact and big wallet
driver.quit()
Largest Contract: 423234543343603.1 Largest Wallet: 10311473240313.781
Answered By - pmadhu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.