简体   繁体   中英

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.

这应该工作:

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

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'}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM