Diamonds
Diamonds

Reputation: 117

Python & BeautifulSoup : Issues with soup.find_all

h1 = soup.find('a', {'class': 'lien-jv topic-title'})['title']
print (h1)

I had no problem to take the value which is in title tag with the soup.find function. But there is multiple tag like that on the page I am parsing, so I've to use the soup.find_all function, and it's not working.

With this code

    h1 = soup.find_all('a', {'class': 'lien-jv topic-title'})['title']
    print (h1)

I had this error

Traceback (most recent call last):
  File "<tmp 1>", line 8, in <module>
    h1 = soup.find_all('a', {'class': 'lien-jv topic-title'})['title']
TypeError: list indices must be integers, not str

Thanks for help.

Upvotes: 0

Views: 196

Answers (2)

Arpit Goyal
Arpit Goyal

Reputation: 2254

You should keep in mind that find_all function returns a list of soup objects by which you have filtered, in your case by a class.

What you are next trying to do is:

h1 = soup.find_all('a', {'class': 'lien-jv topic-title'})['title'] soup.find_all('a', {'class': 'lien-jv topic-title'}) is a list and you are trying to access ['title'] which is wrong.

so the best way to receive title from:

titles = map(lambda soup_object: soup_object['title'], soup.find_all('a', {'class': 'lien-jv topic-title'}))

Upvotes: 1

PRMoureu
PRMoureu

Reputation: 13327

this should work :

results = [a['title'] for a in soup.find_all('a', {'class': 'lien-jv topic-title'})]

Upvotes: 2

Related Questions