pradeep
pradeep

Reputation: 27

Extracting values present between the <Div class> tag using BeautifulSoup

In the below HTML code,i need to extract the string "PMR/SMR Effectiveness" present in the class "txtDetail". But with my code, it is fetching all the text elements. I've seen multiple examples on stackoverflow for this,but nothing works for me.Please let me know what is wrong in my code.

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)

Expected output: PMR/SMR Effectiveness

Actual output: HPPMR/SMR Effectiveness PMR/SMR Effectiveness PMR/SMR Effectiveness Computation Formula PMR/SMR is conducted/reviewed as per recommended frequency

HTML code:

<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>

Upvotes: 1

Views: 94

Answers (1)

Michael Dz
Michael Dz

Reputation: 3844

Because you call .text on i variable which is li tag. I've modified your code a bit:

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)

Upvotes: 1

Related Questions