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

How do i code an exception in a list in python?

I am trying to create a news aggregator that scrapes headlines from NY Times using BeautifulSoup4.

I want to include the first 15 elements with an h3 tag on the site. However, the 9th element with a h3 tag on NY Times is an advertisement.

How can i inlude that out?

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

Heres my code:

ht_r = requests.get("https://www.nytimes.com/")
ht_soup = BeautifulSoup(ht_r.content, 'html.parser')
ht_headings = ht_soup.findAll('h3')
ht_headings = ht_headings[0:15]
ht_news = []

I have tried to do

del ht_headings[9]

However, I am getting this error:

SyntaxError: cannot delete function call

>Solution :

you can try:

ht_headings = ht_headings[:9] + ht_headings[10:]
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