Home > OS >  find() in beautifulsoup4 in python
find() in beautifulsoup4 in python

Time:09-25

I am trying to get the text ("INC000001") from the html code. I have tried many different ways and still unable.

here is what I tried.

soup.find("div", {"data-itrac-control-cd":"CS_ID"}).find("span").text()

I get this error

AttributeError: 'NoneType' object has no attribute 'find'

I have tried also .get_text() no luck

<div id="186164592" data-itrac-item-id="186164592" data-itrac-control-cd="CS_ID" class="ui-controlgroup-controls itrac-displayonly">
  <div>
    <span class="display_only ui-body-j itrac-label-nobodybg" id="186164592">INC000001
    </span>
   </div>
</div>

CodePudding user response:

Try:

soup.find("div", {"data-itrac-control-cd":"CS_ID"}).div.find("span").text()

CodePudding user response:

This is working

from bs4 import BeautifulSoup
html = '<div id="186164592" data-itrac-item-id="186164592" data-itrac-control-cd="CS_ID" class="ui-controlgroup-controls itrac-displayonly"><div><span class="display_only ui-body-j itrac-label-nobodybg" id="186164592">INC000001</span></div></div>'
soup = BeautifulSoup(html)
soup.div.div.span.text

Output:

'INC000001'
  • Related