python - 分析来自魔兽世界 API 的数据
问题描述
想要从 Bizzard 游戏 API 对魔兽世界 Mythic Keystone 运行进行每周分析。内容类型为 json 格式。尝试使用 pandas 数据框读取它,但收到错误消息。有人可以帮忙吗?太感谢了。
import json
import pandas as pd
url = "https://us.api.blizzard.com/data/wow/connected-realm/11/mythic-leaderboard/197/period/641?namespace=dynamic-us&locale=en_US&access_token=USv4TApalDNvvoTftKi4ieXKafqA0UkxM5"
df = pd.read_json(url)
print(df)
> ---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-b30c467d73f5> in <module>
4
5 url = "https://us.api.blizzard.com/data/wow/connected-realm/11/mythic-leaderboard/197/period/641?namespace=dynamic-us&locale=en_US&access_token=USv4TApalDNvvoTftKi4ieXKafqA0UkxM5"
----> 6 df = pd.read_json(url)
7 print(df)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
197 else:
198 kwargs[new_arg_name] = new_arg_value
--> 199 return func(*args, **kwargs)
200
201 return cast(F, wrapper)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
297 )
298 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 299 return func(*args, **kwargs)
300
301 return wrapper
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\json\_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows, storage_options)
561
562 with json_reader:
--> 563 return json_reader.read()
564
565
......
解决方案
对于复杂的数据,我会使用简单的数据结构(list
/ dict
),但如果你想将它加载到 Pandas DataFrame,你可以使用这个例子(使用pd.json_normalize()
):
import requests
import pandas as pd
url = "https://us.api.blizzard.com/data/wow/connected-realm/11/mythic-leaderboard/197/period/641?namespace=dynamic-us&locale=en_US&access_token=USv4TApalDNvvoTftKi4ieXKafqA0UkxM5"
data = requests.get(url).json()
# explode leading_groups
df = pd.json_normalize(data).explode("leading_groups")
df = pd.concat(
[
df["leading_groups"].apply(pd.Series),
df.drop(columns="leading_groups"),
],
axis=1,
)
print(df)
印刷:
ranking duration completed_timestamp keystone_level members period period_start_timestamp period_end_timestamp keystone_affixes map_challenge_mode_id name _links.self.href map.name map.id connected_realm.href
0 1 4701656 1523646535000 25 [{'profile': {'name': 'Volladin', 'id': 186986... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 2 2810578 1523583162000 23 [{'profile': {'name': 'Noru', 'id': 180317150,... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 3 3011261 1523943629000 23 [{'profile': {'name': 'Poodz', 'id': 129866206... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 4 1893872 1523401897000 22 [{'profile': {'name': 'Stigflow', 'id': 183169... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 5 1910501 1523540964000 22 [{'profile': {'name': 'Royally', 'id': 1842199... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 6 2003593 1523424537000 22 [{'profile': {'name': 'Orhdanett', 'id': 17475... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 7 2083673 1523688943000 22 [{'profile': {'name': 'Aiery', 'id': 156862476... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 8 2365147 1523496642000 22 [{'profile': {'name': 'Illidannqt', 'id': 1830... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 9 1660092 1523518737000 21 [{'profile': {'name': 'Shanko', 'id': 18509440... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 10 1996725 1523733590000 21 [{'profile': {'name': 'Plainspoon', 'id': 1580... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 11 2000433 1523781862000 21 [{'profile': {'name': 'Drnkdds', 'id': 1850621... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 12 2022052 1523920103000 21 [{'profile': {'name': 'Shùgo', 'id': 177712580... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 13 2117574 1523923657000 21 [{'profile': {'name': 'Blindeyess', 'id': 1880... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 14 2189980 1523836760000 21 [{'profile': {'name': 'Webangel', 'id': 182353... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 15 2282325 1523412562000 21 [{'profile': {'name': 'Forbídden', 'id': 13204... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 16 2308561 1523951532000 21 [{'profile': {'name': 'Typicaltimm', 'id': 185... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 17 2416497 1523844384000 21 [{'profile': {'name': 'Slashtro', 'id': 173303... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 18 2686569 1523529099000 21 [{'profile': {'name': 'Trexboi', 'id': 1591660... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 19 2779084 1523526569000 21 [{'profile': {'name': 'Shunellx', 'id': 181652... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
0 20 3025735 1523486505000 21 [{'profile': {'name': 'Palymon', 'id': 1791935... 641 1523372400000 1523977199000 [{'keystone_affix': {'key': {'href': 'https://... 197 Eye of Azshara https://us.api.blizzard.com/data/wow/connected... Eye of Azshara 1456 https://us.api.blizzard.com/data/wow/connected...
...and so on.
推荐阅读
- sql - SELECT DISTINCT 省略 ORDER BY 中存在的列
- javascript - 如何根据给定的对象将对象输出到动态变化的表中
- uml - 我如何表示用例的条件?
- python - Jupyter 笔记本:如何执行包含导入和魔术命令的外部文件?
- android - 未找到改造注释。@GET 调用中的(参数 #2)
- c# - 使用外键播种表
- node.js - node-tesseract-ocr 'tesseract' 未被识别为内部或外部命令
- postgresql - PostgreSQL 端口 5432 已在使用 // 忘记用户密码
- android - 无法启动 android 模拟器:WHPX: Unexpected VP exit code 5
- payment-gateway - 如何使用 Authorize.Net 设置定期付款?