Reputation: 111
I have tried to fix this error but I can't.
I am trying to scrape a website and here is how it looks:
the variable str1 contain the web address of the website in the form of string.
my_url = str1
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
data = page_soup.findAll("div", {"class": "_2kHMtA"})
And at last I tried to print this statement:
y = data[2].div.img['alt']
print(y)
When I print this statement it always tries to show that error ! Please Help.
Upvotes: 1
Views: 405
Reputation: 5954
Print your data
object. You haven't managed to find the data you're looking for, so you are trying to print the 3rd entry in nothing (hence the failure).
Without knowing your input html I can't say why that is.
AFAIK you can't pass a dict to findAll
. Rather you use keyword arguments, like so:
soup.find_all("div", class="_myclass")
Note that findAll
is an alias for find_all
.
Note also that you can only run find_all
on a soup object. You can't run it on the results of a previous find_all
object, which is a ResultsSet
object (basically a list of soup objects). (Although, of course, you can iterate over those soup objects if you needed to.)
Upvotes: 3