【问题标题】:BeautifulSoup find class contains some specific wordsBeautifulSoup 查找类包含一些特定的单词
【发布时间】:2019-07-15 03:00:57
【问题描述】:

我四处寻找有关如何查找名称中包含某些单词的类的信息,但没有找到。我想从带有单词页脚的类中获取信息。

<div class="footerinfo"> <span class="footerinfo__header"> </span> </div>

<div class="footer">
    <div class="w-container container-footer">
    </div>
</div>

我试过了,还是不行

soup.find_all('div',class_='^footer^'):

 soup.find_all('div',class_='footer*'):

有人对这样做有任何想法吗?

【问题讨论】:

标签: web-scraping beautifulsoup


【解决方案1】:

您可以使用 CSS 选择器,它允许您根据特定属性的内容选择元素。这包括选择器 *= for contains。

for ele in soup.select('div[class*="footer"]'):
    print (ele)

或正则表达式

import re

regex = re.compile('.*footer.*')
soup.find_all("div", {"class" : regex})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多