【发布时间】:2018-07-30 07:27:11
【问题描述】:
我想选择所有<div>,其中类名是post has-profile bg2或post has-profile bg1,但不是最后一个,即panel
<div id="6" class="post has-profile bg2"> some text 1 </div>
<div id="7" class="post has-profile bg1"> some text 2 </div>
<div id="8" class="post has-profile bg2"> some text 3 </div>
<div id="9" class="post has-profile bg1"> some text 4 </div>
<div class="panel bg1" id="abc"> ... </div>
select() 仅匹配单个匹配项。我正在尝试使用find_all(),但是 bs4 无法找到它。
if soup.find(class_ = re.compile(r"post has-profile [bg1|bg2]")):
posts = soup.find_all(class_ = re.compile(r"post has-profile [bg1|bg2]"))
如何使用正则表达式和不使用正则表达式来解决它?谢谢。
【问题讨论】: