BeautifulSoup找不到所需的div [英] BeautifulSoup can't find required div
问题描述
我一直在尝试获取嵌套的div及其内容,但不能.我想使用class:'box coursebox'访问div.
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'})
上面的代码给出了一个div,其中有0个元素,应该为8.这行之前的find_all调用可以正常工作.
The above code gives a div with 0 elements, when there should be 8. find_all calls before this line work perfectly.
感谢您的帮助!
推荐答案
对于具有多个值的属性,Beautiful Soup会将所有值放入列表中.在您的代码中,查找时需要考虑到这一点.
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.
也许是这样吗?
div = soup.find_all('div', class_="box coursebox"})
请参阅本节的Soup的文档,以获取有关多值属性的更多信息,以及本节,了解有关按类查找元素的详细信息.
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.
这篇关于BeautifulSoup找不到所需的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!