Akari Oozora
Akari Oozora

Reputation: 368

Problems with BeautifulSoup find_all

I need to retrieve a few ids from a site html, it's not a hard work to do if i create some variables to store them there, however i would like to use a list to make it easier to find and work with.

The terminal returns "TypeError: list indices must be integers or slices, not str" when using the following line:

ids = site.find_all('p', class_="frase fr")['id']

I mean, using soup.find_all works fine for me, though if i use the square brackets in the end to specify where it should gather the info it don't work. Here lies the problem, how can i fix it?

Upvotes: 0

Views: 37

Answers (1)

Arthur Pereira
Arthur Pereira

Reputation: 1559

The find_all method returns a list of elements, so if you want to get only the IDs for each element you will have to iterate over each one and extract the desired information.

Use this instead:

ids = [p.get('id') for p in site.find_all('p', class_="frase fr")]

This will give you a list of every ID in the tags you find, including None ones.

You can also filter the None's out using:

ids = [p.get('id') for p in site.find_all('p', class_="frase fr") if p.get('id')]

Upvotes: 1

Related Questions