我正在尝试查找具有 column
类的所有 p
标签。
<p class="column">This is a column</p>
<p class="column">More columns heh</p>
我试过:
soup.find_all(class_='column')
它返回了 []
然后我尝试了:
soup.find_all(attrs={'class': 'column'})
并得到了正确的结果。
这两个语句不应该相同吗?有什么区别?
最佳答案
(这是我对 Stack overflow 的第一个回答,所以我有点紧张!)
正如其他人所说,这两者完全相同。唯一的问题是您使用的是旧版本的 Beautiful Soup。 正如它所说 HERE .
In older versions of Beautiful Soup, which don’t have the class_ shortcut, you can use the attrs trick mentioned above. Create a dictionary whose value for “class” is the string (or regular expression, or whatever) you want to search for.
希望对您有所帮助!
关于python - beautifulsoup find_all() 类快捷方式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430207/