So I've been trying to retrieve some data using BeautifulSoup.
<div >
<div 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 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 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 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.
CodePudding user response:
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 >
<div 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 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 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 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%
CodePudding user response:
If you're using parsel
:
from parsel import Selector
html = """
<div >
<div 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 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 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 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>
"""
selector = Selector(text=html)
# regular for loop
for result in selector.css(".grid.custom_popover::text"):
print(result.get())
# or list comprehension
list_result = "\n".join([result.get() for result in selector.css(".grid.custom_popover::text")])
print(list_result)
Outputs:
40%
35%
20%
5%
40%
35%
20%
5%