问题
I have been trying to get at a nested div and its contents but am not able to. I want to access the div with class:'box coursebox'.
response = res.read()
soup = BeautifulSoup(response, "html.parser")
div = soup.find_all('div', attrs={'class':'box coursebox'})
The above code gives a div with 0 elements, when there should be 8. find_all calls before this line work perfectly.
Thanks for helping!
回答1:
In the case of attributes having more than one value, Beautiful Soup puts all the values into a list. In your code, you need to take this into account when you're doing your lookup.
Perhaps something like this?
div = soup.find_all('div', class_="box coursebox"})
Refer to this section of Beautiful Soup's documentation for more information on multi-valued attributes, and this section for details on looking elements up by class.
Also, please don't post source code as an image.
回答2:
change:
soup = BeautifulSoup(response, "html.parser")
to:
soup = BeautifulSoup(response, "lxml")
html.parser
is not stable, you can change it to lxml
来源:https://stackoverflow.com/questions/42097894/beautifulsoup-cant-find-required-div