rbutrnz
rbutrnz

Reputation: 393

Grabbing dynamic data returns wrong value and a None

The snippet tries to grab 2 data items from a URL however, one returns the wrong value, and the other returns a None.

import requests, time
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('chromedriver.exe')
url = "https://poocoin.app/tokens/0xb081cbaaa86959fc4033fde02bc539c4e897f0a1"
driver.get(url)
time.sleep(8)

soup = BeautifulSoup(driver.page_source, 'lxml')

ts = soup.find('div', class_='px-3')
mc = soup.find('span', class_='text-success')

print (ts)
print (mc.text)
driver.quit()

Current Output:

Total Supply:100,000,000,000Market Cap: (Includes locked, excludes burned)$335,743Pc v2 | BUNDL/BNB LP Holdings:187.41 BNB ($94,329) | Chart | HoldersBUNDL TransactionsBUNDL ContractBUNDL HoldersDev Wallet Checker Bitquery Explorer PooCoin Visits chart 
$2.22

Wanted Output:

Total Supply:
100,000,000,000

Market Cap: (Includes locked, excludes burned)
$238,945

Upvotes: 0

Views: 97

Answers (3)

Ram
Ram

Reputation: 4779

You can use .stripped_strings and select the required data from the list of strings.

import requests, time
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('chromedriver.exe')
url = "https://poocoin.app/tokens/0xb081cbaaa86959fc4033fde02bc539c4e897f0a1"
driver.get(url)
time.sleep(8)

soup = BeautifulSoup(driver.page_source, 'lxml')

ts = soup.find('div', class_='px-3')
x = list(ts.stripped_strings)[:5]
print(f'{x[0]}\n{x[1]}\n\n{x[2]}{x[3]}\n{x[4]}')

driver.quit()
Total Supply:
100,000,000,000

Market Cap:(Includes locked, excludes burned)
$331,584

Upvotes: 2

Md. Fazlul Hoque
Md. Fazlul Hoque

Reputation: 16187

Try: Hope,it will help you. Acurate selection.

ts = soup.select_one('div.text-small>div.px-3')
mc = soup.select_one('div.text-small>div.px-3 >span.text-success')

print (ts.text)
print (mc.text)

Upvotes: 1

jgmh
jgmh

Reputation: 689

You can access the data using the contents method. This way you can get the required info:

ts = soup.find('div', class_='px-3')
print(ts.contents[0])
print(ts.contents[2])

Which will output:

Total Supply:
100,000,000,000

Using the same procedure you can extract the remaining data.

Upvotes: 1

Related Questions