Beautiful Soup Selector

问题描述 投票:0回答:1

是Beautiful Soup的新功能,在下面的代码中一直坚持从“数据”中获取两个值。理想情况下,我想选择value1(500)作为“ item1”,第二个值(442)作为“ item2”。

<div  id="chart-1" class="charts-highchart"  data-chart="{&quot;chart&quot;:{&quot;type&quot;:&quot;pie&quot;,&quot;width&quot;:null,&quot;height&quot;:null,&quot;backgroundColor&quot;[&quot;Male&quot;,&quot;Female&quot;],&quot;data&quot;:[500,442]}],&quot;exporting&quot;pane&quot;:null}"
        style=""></div>
python beautifulsoup css-selectors
1个回答
0
投票

使用正则表达式re并使用以下css选择器。

import re
from bs4 import BeautifulSoup

html='''<div  id="chart-1" class="charts-highchart"  data-chart="{&quot;chart&quot;:{&quot;type&quot;:&quot;pie&quot;,&quot;width&quot;:null,&quot;height&quot;:null,&quot;backgroundColor&quot;[&quot;Male&quot;,&quot;Female&quot;],&quot;data&quot;:[500,442]}],&quot;exporting&quot;pane&quot;:null}"
        style=""></div>'''
soup=BeautifulSoup(html,'html.parser')
data=soup.select_one('#chart-1[data-chart]')['data-chart']
items=re.findall("(\d+)",data)
for item in items:
    print(item)

输出

500
442

如果要在变量中使用,请使用此。

import re
from bs4 import BeautifulSoup

html='''<div  id="chart-1" class="charts-highchart"  data-chart="{&quot;chart&quot;:{&quot;type&quot;:&quot;pie&quot;,&quot;width&quot;:null,&quot;height&quot;:null,&quot;backgroundColor&quot;[&quot;Male&quot;,&quot;Female&quot;],&quot;data&quot;:[500,442]}],&quot;exporting&quot;pane&quot;:null}"
        style=""></div>'''
soup=BeautifulSoup(html,'html.parser')
data=soup.select_one('#chart-1[data-chart]')['data-chart']
items=re.findall("(\d+)",data)
item1=items[0]
item2=items[-1]
print(item1,item2)
© www.soinside.com 2019 - 2024. All rights reserved.