在看漫威系列电影的时候,你是不是经常会对一些角色感到好奇,想知道每个角色的关联关系和出场的事件,但是却无从下手?
现在,我们有很好的库来帮助我们实现这些想法了!Marvel Comics API 允许各地的开发人员访问漫威70年来庞大的漫画库信息。接下来就来告诉大家怎么使用这个漫威库。
1.注册账号获得API访问权限
访问下面的链接注册一个漫威开发者
https://developer.marvel.com/signup
跟着步骤走,接受它的使用条款后就能得到public key(公钥)和 private key(私钥)了:
2.pip安装相应的第三方工具
在pip中输入以下命令安装marvel包(默认你已经安装好了python和pip哦,如果你还没有安装,建议阅读这个教程:python安装)
pip install marvel
这个包是这个漫威API的封装器,里面封装了许多查询工程,方便我们使用,我们不需要理解怎么发送post请求向漫威api调用数据,仅仅使用一个语句,将我们刚刚获得的公钥和私钥传入进去,就可以拿到数据。
公钥秘钥示例
获取所有角色:
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) characters = m.characters all_characters = characters.all() print(all_characters)
获取单个角色:
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) characters = m.characters character = characters.get(1011334) print(character)
获取一些角色的漫画:
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) characters = m.characters comics = characters.comics(1011334) print(comics)
同样,您可以将相同的逻辑应用于不同的对象,例如:
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) stories = m.stories all_stores = stories.all() story = stories.get(id) events = stories.events(id) print(stories, all_stores, story, events)
最后,每个对象具有的子资源如下:
- 人物
all
,get
,comics
,events
,series
,stories
- 漫画
all
,get
,characters
,creators
,events
,stories
- 创作者
all
,get
,comics
,events
,series
,stories
- 活动
all
,get
,characters
,comics
,creators
,series
,stories
- 系列
all
,get
,characters
,comics
,creators
,events
,stories
- 故事
all
,get
,characters
,comics
,creators
,events
,series
3. 使用API找到雷神出现过的漫画
想要使用API查找灭霸出现过的所有漫画,你就得先知道雷神的角色ID(character ID), 我们通过角色名字得到角色对应的ID:
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) characters = m.characters def get_hero_id(characters, name): all_characters = characters.all(nameStartsWith=name) # 根据名字获得角色信息,仅支持英文 ids = [i['id'] for i in all_characters['data']['results']] names = [i['name'] for i in all_characters['data']['results']] return ids,names ids, names = get_hero_id(characters, 'thor')
结果:
(base) ckenddeMacBook-Pro:20190925 ckend$ python 1.py [1009664, 1017576, 1017106, 1017315, 1017328, 1017302, 1011025, 1010820] ['Thor', 'Thor (Goddess of Thunder)', 'Thor (MAA)', 'Thor (Marvel Heroes)', 'Thor (Marvel War of Heroes)', 'Thor (Marvel: Avengers Alliance)', 'Thor (Ultimate)', 'Thor Girl']
可以看到我们好像得到了不同系列下的雷神,以1009664为例,获得雷神出现过的漫画。
import marvel PUBLIC_KEY = '你的公钥' PRIVATE_KEY = '你的私钥' m = marvel.Marvel(PUBLIC_KEY, PRIVATE_KEY) characters = m.characters def get_hero_id(characters, name): all_characters = characters.all(nameStartsWith=name) # 根据名字获得角色信息,仅支持英文 ids = [i['id'] for i in all_characters['data']['results']] names = [i['name'] for i in all_characters['data']['results']] return ids,names ids, names = get_hero_id(characters, 'thor') comics = characters.comics(ids[0]) # ids[0]即1009664 print([i['title'] for i in comics['data']['results']])
结果如下:
(base) ckenddeMacBook-Pro:20190925 ckend$ python 1.py ['THOR VOL. 2: ROAD TO WAR OF THE REALMS TPB (Trade Paperback)', 'Marvel Masterworks: The Mighty Thor Vol. 18 (Hardcover)', 'King Thor (2019) #1', 'Thor Epic Collection: The Black Galaxy (Trade Paperback)', 'Thor (2018) #16', 'THOR & LOKI: BLOOD BROTHERS GALLERY EDITION HC (Hardcover)', 'Thor Of The Realms (Trade Paperback)', 'War Of The Realms Omega (2019) #1', 'Thor (2018) #15', 'The Unbeatable Squirrel Girl (2015) #46', 'Kirby Is… Mighty! King-Size (Hardcover)', 'Thor (2018) #14', 'War of the Realms (2019) #5', 'MARVEL ACTION CLASSICS: SPIDER-MAN TWO-IN-ONE 1 (2019) #1', 'Thor (2018) #13', 'Moon Girl and Devil Dinosaur (2015) #43', "Decades: Marvel in The '80s - Awesome Evolutions (Trade Paperback)", 'War of the Realms (2019) #3', 'The Art of War of the Realms (Trade Paperback)', 'Mighty Thor 3D (2019) #1']
可以看到,雷神一共在十九部作品里出现过哦。怎么样,是不是特别方便的工具?
我们的文章到此就结束啦,如果你希望我们今天的Python 教程,请持续关注我们,如果对你有帮助,麻烦在下面点一个赞/在看哦
Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典