在下面的HTML代码中,我需要提取在“ txtDetail”类中存在的字符串“ PMR / SMR有效性”。但是用我的代码,它正在获取所有文本元素。我已经在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,&quot;drill1&quot;,&quot; AND m.MetricId=20&quot;,&quot;2&quot;,&quot;PMR/SMR Effectiveness&quot;,&quot;20&quot;,&quot;286&quot;,&quot;0&quot;,&quot;0&quot;)" 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>

最佳答案

因为您在.text标记的i变量上调用li。我对您的代码做了一些修改:

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/

10-11 08:42