How can I remove the p tag of the data being grabbed

Viewed 56

I am trying the snippet to grab the p inside the div. When running the script, the output includes its all formatting tags.

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

driver = webdriver.Chrome('chromedriver.exe')
url = 'https://poocoin.app/rugcheck/0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66/dev-activity'
driver.get(url)

time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'lxml')
pdata = soup.find_all('div',attrs={"class":"mt-2"})
for x in pdata:
    print (x.find('p'))
driver.quit()

Current Output:

<p><a href="/tokens/0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66">Go to chart</a></p>
<p>This is a log of activity related to the token from all wallets that have had ownership of the contract.</p>
<p>Wallet activity for <a href="https://bscscan.com/address/0x410e372657e088d5b7db76346cd958b1b642b984" rel="noreferrer" target="_blank">0x410e372657e088d5b7db76346cd958b1b642b984</a><br/><span class="text-muted text-small">(Ownership transferred to <a href="https://bscscan.com/address/0x0000000000000000000000000000000000000000" rel="noreferrer" target="_blank">0x0000000000000000000000000000000000000000</a> on 4/17/2021, 4:59:30 AM)</span></p>

Wanted Output:

0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66
Wallet activity for 0x410e372657e088d5b7db76346cd958b1b642b984
(Ownership transferred to 0x0000000000000000000000000000000000000000 on 17/04/2021, 4:59:30 am)
2 Answers

Try:

pdata = soup.select('div.mt-2 p )
for x in pdata:
    print (x.text)

You can do this with regular expressions:

from bs4 import BeautifulSoup
from selenium import webdriver
import time
import re

driver = webdriver.Chrome('chromedriver.exe')
url = 'https://poocoin.app/rugcheck/0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66/dev-activity'
driver.get(url)

time.sleep(5)
soup = BeautifulSoup(driver.page_source, 'lxml')
pdata = soup.find_all('div',attrs={"class":"mt-2"})
lines = [str(x.find('p')) for x in pdata]

address = re.search('/tokens/(0x\w+)"', lines[1]).group(1)
print(address)

activity = 'Wallet activity for ' + re.search('/address/(0x\w+)"', lines[3]).group(1)
print(activity)

matches = re.search('"_blank">(0x\w+)</a>( on [^\)]+)\)', lines[3])
ownership = '(Ownership transferred to ' + matches.group(1) + matches.group(2) + ')'
print(ownership)

driver.quit()

Output:

0xf09b7b6ba6dab7cccc3ae477a174b164c39f4c66
Wallet activity for 0x410e372657e088d5b7db76346cd958b1b642b984
(Ownership transferred to 0x0000000000000000000000000000000000000000 on 16/04/2021, 21:59:30)
Related