user5303326
user5303326

Reputation:

Python BeautifulSoup: How to get TEXT from <td>TEXT</td>

i can't figure it out how to get TEXT and NUMBERS from this tag <td>THERE IS TEXT I WANT TO GET</td> and there is "Quantity" also with <td>QUANTITY</td>

link:https://bscscan.com/tokenholdings?a=0x00a2c3d755c21bc837a3ca9a32279275eae9e3d6

there is image what i want to get.

thanks in advance

enter image description here

Upvotes: 1

Views: 4457

Answers (3)

Sushil
Sushil

Reputation: 5531

The table in the website is loaded dynamically, so you can't scrape it using requests. You have to use selenium in order to do it. Here is the full code:

from bs4 import BeautifulSoup
from selenium import webdriver
import time
import pandas as pd

url = 'https://bscscan.com/tokenholdings?a=0x00a2c3d755c21bc837a3ca9a32279275eae9e3d6'

driver = webdriver.Chrome()

driver.get(url)

time.sleep(5)

html = driver.page_source

driver.close()

soup = BeautifulSoup(html,'html5lib')

tbody = soup.find('tbody', id = "tb1")

tr_tags = tbody.find_all('tr')

symbols = []
quantities = []

for tr in tr_tags:
    td_tags = tr.find_all('td')
    symbols.append(td_tags[2].text)
    quantities.append(td_tags[3].text)

df = pd.DataFrame((symbols,quantities))

df = df.T

df.columns = ['Symbol','Quantity']

print(df)

Output:

  Symbol      Quantity
0    BNB   17.98420742
1   Cake   19.76899295
2    ANY             1
3   FREE         1,502
4    LFI  326.87340092
5    LFI  326.87340092

Upvotes: 4

Ahmad H. Ibrahim
Ahmad H. Ibrahim

Reputation: 1079

>>> html="<td>THERE IS TEXT I WANT TO GET</td>\n<td>THERE IS TEXT I WANT TO GET</td>\n<td>THERE IS TEXT I WANT TO GET</td>\n<td>THERE IS TEXT I WANT TO GET</td>"
>>> soup = BeautifulSoup(html)
>>> for td in soup.find_all('td'): print(td.text)

Upvotes: 0

min20120907
min20120907

Reputation: 186

I recommend a really good tool called re, and you can search the specific string from two substrings, e.g.

import re

s = ''<td>THERE IS TEXT I WANT TO GET</td>"
result = re.search('<td>(.*)</td>', s)
print(result.group(1))

Upvotes: 0

Related Questions