小编use*_*572的帖子

Beautiful Soup的Python正则表达式

我使用Beautiful Soup来提取特定的div标签,似乎我不能使用简单的字符串匹配.

该页面有一些标签形式

<div class="comment form new"...> 
Run Code Online (Sandbox Code Playgroud)

我想忽略它,还有一些标签的形式

<div class="comment comment-xxxx..."> 
Run Code Online (Sandbox Code Playgroud)

其中x表示任意长度的整数,椭圆表示由空格分隔的任意数量的其他值(我不关心).我无法弄清楚正确的正则表达式,特别是因为我从未使用过python的re class.

运用

soup.find_all(class_="comment") 
Run Code Online (Sandbox Code Playgroud)

查找以单词comment开头的所有标签.我试过用

soup.find_all(class_=re.compile(r'(comment)( )(comment)'))
soup.find_all(class_=re.compile(r'comment comment.*'))
Run Code Online (Sandbox Code Playgroud)

还有很多其他变种,但我想我在这里遗漏了一些关于正则表达式或match()如何工作的东西.谁能帮我吗?

python regex beautifulsoup

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

beautifulsoup ×1

python ×1

regex ×1