Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bs4 remove None tag after decompose

I want to delete advertisment text from scraped data but after i decompose it i get error saying

list index out of range

I think its becouse after decompose is blank space or somthing. Without decompose loop works ok.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import requests
from bs4 import BeautifulSoup

url = 'https://www.marketbeat.com/insider-trades/ceo-share-buys-and-sales/'

companyName = 'title-area'

r = requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')

table = soup.find_all('table')[0].tbody.find_all('tr')

#  delete advertisment
soup.find("tr", class_="bottom-sort").decompose()

for el in table:
    print(el.find_all('td')[0].text)

>Solution :

You can use tag.extract() to delete the tag. Also, delete the tag before you find all <tr> tags:

import requests
from bs4 import BeautifulSoup

url = "https://www.marketbeat.com/insider-trades/ceo-share-buys-and-sales/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

#  delete advertisment
for tr in soup.select("tr.bottom-sort"):
    tr.extract()

table = soup.find_all("table")[0].tbody.find_all("tr")

for el in table:
    print(el.find_all("td")[0].text)

Prints:


...
TZOOTravelzoo
NEOGNeogen Co.
RKTRocket Companies, Inc.
FINWFinWise Bancorp
WMPNWilliam Penn Bancorporation
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading