Rafael Martins Rafael Martins - 1 month ago 17
HTML Question

Extract the string after title in BeautifulSoup

html result is

<div class="font-160 line-110" data-container=".snippet container" data-html="true" data-placement="top" data-template='&lt;div class="tooltip infowin-tooltip" role="tooltip"&gt;&lt;div class="tooltip-arrow"&gt;&lt;div class="tooltip-arrow-inner"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="tooltip-inner" style="text-align: left"&gt;&lt;/div&gt;&lt;/div&gt;' data-toggle="tooltip" title="XIAMEN [CN]">


How do I pull out
"XIAMEN [CN]"
right after
title
. I tried
find_all('title')
but that does not return a match. Nor can I call any from of
siblings
to traverse my way down the result. I couldn't even get
find(text='XIAMEN [CN]')
to return anything.

Answer
from bs4 import BeautifulSoup

myHTML = 'what you posted above'
soup = BeautifulSoup(myHTML, "html5lib")
title = soup.find('div')['title']

We're just searching for <div> tags here, you'll probably want to be more specific in vivo.

Comments