python - BS4 find_all 在空间中带有标签

标签 python python-3.x beautifulsoup tags

如何将 bs4 与带有空格的类标签的 find_all 一起使用?

container = containers[0]
product_container = container.find_all('div',{'class': 's-item-container'})
product_name = product_container.find_all('div', {'class': 'a-fixed-left-grid-col'})
print (product_name)

div 类标签是“a-fixed-left-grid-col a-col-right”,如何传递find_all 函数?

最佳答案

可以直接使用带空格的类名作为class的值如果您想要的标签格式为 <tag class="classname1 classname2 ..."> .

soup.find_all('tag', {'class': 'classname1 classname2 ...'})

您还可以使用类列表:

soup.find_all('tag', {'class': ['class1', 'class2']})

但是,第二种方法将匹配以下类型的所有标签:

  • class="class1"
  • class="class2"
  • class="class1 class2"
  • class="class2 class1" (这和上面完全一样)

关于python - BS4 find_all 在空间中带有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49424015/

相关文章:

python - python是否有相当于Javascript的 'btoa'

python - 有没有办法在同一个测试中参数化具有多种数据类型的 pytest?

python - UnicodeEncodeError : 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

python - 如何在读取的 HTML 文档中翻译/转换 unicode 转义 < 和 >?

python - python 3中列表的合并列表

python - 改善 BeautifulSoup 性能

python - 如何在 Visual Studio Code 中调试 Python 3 代码?

python - 具有多个列表乘法运算符的单个列表的紧凑生成

python - 如何以特殊方式对 Pandas 数据框进行排序

python-3.x - Python 搜索以模式 dn 开头的行并使用 oteh 子模式打印接下来的 2 行