美丽的汤(python)获得属性的价值

时间:2014-03-12 11:29:56

标签: python beautifulsoup

我有一些凌乱的汤,我一直试图解析,我无法弄清楚我会怎么做。在页面上有一堆<div>标签,我可以成功遍历它们以找到我想要的div。

这个div有一个名为"data-series"的自定义属性,其值似乎是一些包含列表的字典列表。 data-series属性的值如下所示:

<div data-series=
'[{"label":"Series 1","data":[[0,0.01214697],[1,0.01139803],[2,0.0101848]],"color":"#27a9e3"},
{"label":"series 2","data":[[0,0.00745604375],[1,0.00885196875],[2,0.009824050833]],"color":"#ffb848"}]'....

然后继续使用其他一些自定义属性。我想要在这个嵌套的混乱中提取一个数字

我想要打印出的值是0.01139803。在列表中,它位于第一个字典中,并且是"data"键的值。但是"data"键的值本身就是一个列表,并且是第二个嵌套元素的第二个元素([1][1]

我怎么用漂亮的汤来取出这个数字?

1 个答案:

答案 0 :(得分:4)

data-series的字符串是“JSON”(JavaScript Object Notation)数据。您可以使用json.loads()将此字符串处理为Python数据结构,然后像处理任何listdict一样处理结果:

>>> import json
>>> s = '[{"label":"Series 1","data":[[0,0.01214697],[1,0.01139803],[2,0.0101848]],"color":"#27a9e3"},{"label":"series 2","data":[[0,0.00745604375],[1,0.00885196875],[2,0.009824050833]],"color":"#ffb848"}]'
>>> d = json.loads(s)
>>> d[0]['data'][1][1]
0.01139803