Issue
So I've been trying to retrieve some data using BeautifulSoup.
<div class="chartAreaContainer spm-bar-chart">
<div class="grid custom_popover" data-content="<b>Advertising</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 40%" title="">40%</div>
<div class="grid custom_popover" data-content="<b>Media Planning & Buying</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 35%" title="">35%</div>
<div class="grid custom_popover" data-content="<b>Branding</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 20%" title="">20%</div>
<div class="grid custom_popover" data-content="<b>Event Marketing & Planning</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 5%" title="">5%</div>
</div>
How to get their data-content name and their percentage.
I am trying .text
but it gives only the percentage.
Solution
Simply select the attribute of the element by its name ['data-content']
- Cause question is not that detailed, this answer just point into direction.
Example
html = '''
<div class="chartAreaContainer spm-bar-chart">
<div class="grid custom_popover" data-content="<b>Advertising</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 40%" title="">40%</div>
<div class="grid custom_popover" data-content="<b>Media Planning & Buying</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 35%" title="">35%</div>
<div class="grid custom_popover" data-content="<b>Branding</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 20%" title="">20%</div>
<div class="grid custom_popover" data-content="<b>Event Marketing & Planning</b>" data-html="true" data-original-title="" data-placement="top" data-toggle="popover" data-trigger="hover" role="button" style="width: 5%" title="">5%</div>
</div>
'''
soup = BeautifulSoup(html)
for e in soup.select('.custom_popover'):
print(f"{e['data-content']}: {e.text}")
Output
<b>Advertising</b>: 40%
<b>Media Planning & Buying</b>: 35%
<b>Branding</b>: 20%
<b>Event Marketing & Planning</b>: 5%
Answered By - HedgeHog
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.