在下面的 HTML 代码中,我需要提取“txtDetail”类中存在的字符串“PMR/SMR Effectness”。但在我的代码中,它正在获取所有文本元素。我在 stackoverflow 上看到过很多这样的例子,但对我来说没有任何作用。请让我知道我的代码出了什么问题。
soup = BeautifulSoup(driver.page_source,'html.parser')
for i in soup.find_all(class_ ='default yellowBorder Hclass ' ):
if soup.find_all('div',attrs={"class" : "txtDetail"}):
print(i.text)
预期输出:PMR/SMR 有效性
实际输出: HPPMR/SMR 有效性 PMR/SMR 有效性 PMR/SMR 有效性计算公式 PMR/SMR 按照建议频率进行/审核
HTML 代码:
<ul class="blockList" id="ulexceptionlist"><li class="default yellowBorder Hclass "><div class="blockCont"> <div class="yellowBlock dispBlock expPoints"><div class="iconException">H</div><div class="yellowSection">P</div></div><div class=" nextCont textCont activeCont " onclick="loadMerticchart(this,"drill1"," AND m.MetricId=20","2","PMR/SMR Effectiveness","20","286","0","0")" descopereason=""><div class="txtDetail">PMR/SMR Effectiveness</div></div><div class="infoIcon " id="info1" data-hasqtip="4"></div><div class="col-xs-12 noPadding popupContainer"> <div class="col-md-12 dropdownContent pull-left"> <span class="clearfix"></span> <span class="pull-left content"><h6 class="tooltipTitle"> PMR/SMR Effectiveness </h6> <span class="tooltipCont"> PMR/SMR Effectiveness </span><br><br><h6 class="tooltipTitle"> Computation Formula </h6> <span class="tooltipCont"> PMR/SMR is conducted/reviewed as per recommended frequency </span></span> </div></div></div></li>
最佳答案
因为您在 i
变量(即 li
标记)上调用 .text
。我稍微修改了你的代码:
for i in soup.find_all(class_ ='default yellowBorder Hclass ' ):
divs = soup.find_all('div',attrs={"class" : "txtDetail"})
for d in divs:
print(d.text)
关于python - 使用 BeautifulSoup 提取 <Div class> 标记之间存在的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47158548/