Home > Blockchain >  Find data using beautiful soup
Find data using beautiful soup

Time:03-31

So I've been trying to retrieve some data using BeautifulSoup.

<div >
    <div  data-content="&lt;b&gt;Advertising&lt;/b&gt;" 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="&lt;b&gt;Media Planning &amp; Buying&lt;/b&gt;" 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="&lt;b&gt;Branding&lt;/b&gt;" 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="&lt;b&gt;Event Marketing &amp; Planning&lt;/b&gt;" 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="&lt;b&gt;Advertising&lt;/b&gt;" 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="&lt;b&gt;Media Planning &amp; Buying&lt;/b&gt;" 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="&lt;b&gt;Branding&lt;/b&gt;" 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="&lt;b&gt;Event Marketing &amp; Planning&lt;/b&gt;" 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="&lt;b&gt;Advertising&lt;/b&gt;" 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="&lt;b&gt;Media Planning &amp; Buying&lt;/b&gt;" 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="&lt;b&gt;Branding&lt;/b&gt;" 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="&lt;b&gt;Event Marketing &amp; Planning&lt;/b&gt;" 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%
  • Related