美丽的汤选择器

时间:2020-04-27 17:11:29

标签: python beautifulsoup css-selectors

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>

1 个答案:

答案 0 :(得分: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)