副标题[/!--empirenews.page--]
最近正值复联4上映,小F也发现了一个有趣的网站。
主要是关于漫威人物、漫威电影的图谱。
https://graphics.straitstimes.com/STI/STIMEDIA/Interactives/2018/04/marvel-cinematic-universe-whos-who-interactive/index.html(复制到浏览器打开)
网站是基于Graph技术开发的。
其实之前小F也利用了有关Graph的库实现了一波人物的关系分析。
只不过分析结果比较粗糙而已~
下面是网站的概况,大家可以一览。


那么人家能做出这么酷炫的关系图,我们自己能不能实现呢?
这一期就利用网站提供的数据,使用Neo4j(NOSQL图形数据库)进行实战一波。
一、获取分析
人物及人物关联信息从网站上获取,具体接口如下。

数据为json格式,分别在「characters」和「relationship」中。

这里的信息是分别指托尼·斯达克,关系「0」为朋友,斯蒂文·罗杰斯。

二、 数据获取
具体代码如下。
- headers = {
- 'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
- }
-
- url = 'https://graphics.straitstimes.com/STI/STIMEDIA/Interactives/2018/04/marvel-cinematic-universe-whos-who-interactive/data/marvel-data.json'
- response = requests.get(url=url, headers=headers)
- result = json.loads(response.text)
-
- num = 0
- names = []
- item = {0: 'friend', 1: 'enemy', 2: 'creation', 3: 'family', 4: 'work', 5: 'love'}
-
- for i in result['relationship']:
- subject = result['relationship'][i]['id']
- object = result['relationship'][i]['target_id']
-
- if subject not in names:
- names.append(subject)
- if object not in names:
- names.append(object)
-
- relation = int(result['relationship'][i]['relationship'])
- with open('relation_message.csv', 'a+') as f:
- f.write(subject + ',' + object + ',' + item[relation] + 'n')
-
- for j in names:
- num += 1
- with open('names_message.csv', 'a+') as f:
- f.write(j + ',' + str(num) + 'n')
-
- for k in result['characters']:
- id = result['characters'][k]['id']
- name = result['characters'][k]['name']
- status = result['characters'][k]['status']
- species = result['characters'][k]['species']
- with open('message.csv', 'a+') as f:
- f.write(id + ',' + name + ',' + status + ',' + species + 'n')
(编辑:PHP编程网 - 黄冈站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|