Issue
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Bangalore','value':'Bangalore','CID':'105','id':'105','P':'1','slug':'bangaluru'}" style="line-height: initial;"> Bangalore</li>
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Chennai','value':'Chennai','CID':'106','id':'106','P':'2','slug':'madras'}" style="line-height: initial;"> Chennai</li>
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Mumbai','value':'Mumbai','CID':'108','id':'108','P':'3','slug':'bombay'}" style="line-height: initial;"> Mumbai</li>
I want the slug
value from data-gs-ta-value
from each and every element using beautiful soup python.
Solution
You don't say how you're getting the HTML fragment so I'll assume you have it as a string.
data-gs-ta-val is interesting because it looks like the associated datum is a string representation of a Python dictionary.
Therefore:
from bs4 import BeautifulSoup as BS
from ast import literal_eval
html = """
<!DOCTYPE html>
<html>
<body>
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Bangalore','value':'Bangalore','CID':'105','id':'105','P':'1','slug':'bangaluru'}" style="line-height: initial;"> Bangalore</li>
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Chennai','value':'Chennai','CID':'106','id':'106','P':'2','slug':'madras'}" style="line-height: initial;"> Chennai</li>
<li class="gs_ta_choice" data-value="Bangalore" data-gs-ta-val="{'text':'Mumbai','value':'Mumbai','CID':'108','id':'108','P':'3','slug':'bombay'}" style="line-height: initial;"> Mumbai</li>
</body>
</html>
"""
soup = BS(html, 'lxml')
for li in soup.find_all('li', class_='gs_ta_choice'):
d = literal_eval(li['data-gs-ta-val'])
print(d.get('slug', 'No slug here'))
Output:
bangaluru
madras
bombay
Answered By - Codist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.