我如何从该网页中仅提取特定文本?

时间:2020-03-13 02:41:50

标签: python python-3.x

我正在寻找采用以下代码行的方法:

{"id":"76561198170104957","names":[{"name":"Mountain Dew"},{"name":"Sugardust"}],"kills":2394,"deaths":2617,"ff_kills":89,"ff_deaths":110,"playtime":"P5DT3H45M18S"}

,仅将“ kills,deaths,ff_kills和ff_deaths字符串及其关联数字提取到列表中。此代码的长度取决于用户,因此我认为静态索引不会真正起作用。如果有可能,还可以从网页上读取代码。谢谢。

1 个答案:

答案 0 :(得分:3)

该格式称为JSON。您可以使用python轻松解析它。示例:

import json
line = r'{"id":"76561198170104957","names":[{"name":"Mountain Dew"},{"name":"Sugardust"}],"kills":2394,"deaths":2617,"ff_kills":89,"ff_deaths":110,"playtime":"P5DT3H45M18S"}'
j = json.loads(line)
print(j['kills']);