Home > Software design >  How to select element by class and get text?
How to select element by class and get text?

Time:11-01

I have been trying to scrape addresses in this page: https://www.worldometers.info/coronavirus/#news

How can I get values which under class=sorting_1? It is difficult for me. I'm completely new to Beautifulsoup.

1

CodePudding user response:

Using find:

[t.get_text(strip=True) for t in soup.find_all(attrs={'class': 'sorting_1'})]

or if you're sure that's the only class for the tags you want:

[t.get_text(strip=True) for t in soup.find_all(class_='sorting_1')]

or using select

[t.get_text(strip=True) for t in soup.select('.sorting_1')]

Any of the above should work; and if you're going to be working with BeautifulSoup, you should really familiarize yourself with the documentation and/or go through at least one tutorial.

  • Related