BeautifulSoup can't find required div

不想你离开。 提交于 2021-02-10 07:01:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!