# Beautifulsoup find_all returns TypeError: list indices must be integers or slices, not str [closed]

This should have an easy solution but I can't understand how to avoid it.

    content1 = soup.find('div', class_='fusion-text fusion-text-6')
content2 = soup.find('div', class_='fusion-text fusion-text-7')

for para in content1:
comp_name = para.find_all('a')['href']
print(comp_name)


The error comes because I have a list at ['href'] together with find_all.

        comp_name = para.find('a')['href']


doesn't return an error and gives the right output (an URL), but just the first one. Since I want to scrape all of them inside my content1 I wanted to use find_all but inevitably I get the error.

How can I avoid this?

Thank you.

• if the result is a list, you can access list elements like mylist[0] and you can pull out elements from 0 to len(mylist) – Peter Jul 29 '20 at 16:25
• @Peter I'm not sure if I understood it. How can I access it if it returns an error? I don't know how to create a list out of find_all. Thanks. – Steven Jul 29 '20 at 16:34
• comp_name = para.find_all('a')[0].get('href') or comp_name = para.find('a')['href'] return 1 result, but I can't get all of them. – Steven Jul 29 '20 at 17:52

This will give you list of companies then you need an other loop/or list comprehension to extract from the children.

 for para in content1:
comp_name = para.find_all('a')
all_comps = [one_para.get("href") for one_para in para]
print(all_comps )

• this is a copy of my answer :( – Nikos M. Jul 29 '20 at 20:45
• @Nauman thanks, I kinda get what you wrote but I get this: AttributeError: 'NavigableString' object has no attribute 'get' – Steven Jul 29 '20 at 21:15

Use list comprehensions:

comp_name = [link['href'] for link in para.find_all('a')]

• thanks, it works but I can't get the text, only html. I tried .text but as usual find_all doesn't accept that. – Steven Jul 29 '20 at 21:17
• @Steven, change link['href'] to link.get_text() to get the text – Nikos M. Jul 29 '20 at 22:43
• alright, thanks. Just a note, with that from url-html I get the other text at the end of the tag and not urls anymore – Steven Jul 30 '20 at 6:32