0

I have such HTML code:

<li class="IDENTIFIER"><h5 class="hidden">IDENTIFIER</h5><p>
<span class="tooltip-iws" data-toggle="popover" data-content="SOME TEXT">
other text</span></p></li>

And I'd like to obtain the SOME TEXT from the data-content.

I wrote

target = soup.find('span', {'class' : 'tooltip-iws'})['data-content']

to get the span, and I wrote

identifier_elt= soup.find("li", {'class': 'IDENTIFIER'})

to get the class, but I'm not sure how to combine the two.

But the class tooltip-iws is not unique, and I would get extraneous results if I just used that (there are other spans, before the code snippet, with the same class) That's why I want to specify my search within the class IDENTIFIER. How can I do that in BeautifulSoup?

AndW
  • 726
  • 6
  • 31

2 Answers2

1

try using css selector,

soup.select_one("li[class='IDENTIFIER'] > p > span")['data-content']
sushanth
  • 8,275
  • 3
  • 17
  • 28
0

Try using selectorlib, should solve your issue, comment if you need further assistance

https://selectorlib.com/

  • Link-only answers are generally [frowned upon](http://meta.stackexchange.com/a/8259/204922) on Stack Overflow. In time it is possible for links to atrophy and become unavailable, meaning that your answer is useless to users in the future. It would be best if you could provide the general details of your answer in your actual post, citing your link as a reference. – sushanth Apr 21 '21 at 17:05