Python爬蟲(chóng)爬取王者榮耀英雄信息并保存到圖數(shù)據(jù)庫(kù)的操作方法
爬取信息說(shuō)明
- 英雄名稱(chēng)
- 英雄類(lèi)型
- 英雄包含的所有皮膚名稱(chēng)
創(chuàng)建英雄類(lèi)型節(jié)點(diǎn)
王者榮耀官方給出的英雄類(lèi)型是以下幾種:
直接準(zhǔn)備好英雄類(lèi)型詞典
hero_type_dict = [ '戰(zhàn)士', '法師', '坦克', '刺客', '射手', '輔助' ]
添加到圖數(shù)據(jù)庫(kù)中
def create_hero_type_node(): for hero_type in hero_type_dict: cypher = "MERGE (n:HeroType{label: '" + hero_type + "'})" graph.run(cypher).data() print('創(chuàng)建英雄類(lèi)型節(jié)點(diǎn)成功')
創(chuàng)建英雄信息節(jié)點(diǎn)
獲取英雄信息
def get_hero_info_list(): # 英雄的全部信息的url hero_info = 'https://pvp.qq.com/web201605/js/herolist.json' # 獲取英雄的全部信息 response = requests.get(hero_info) # 轉(zhuǎn)為字典格式 hero_info_dict = json.loads(response.text) return hero_info_dict
打印的內(nèi)容如下:
這里需要注意的是,部分英雄包含兩個(gè)英雄類(lèi)別。
保存英雄信息
def create_hero_node(): hero_info_dict = get_hero_info_list() # 1戰(zhàn)士 2法師 3坦克 4刺客 5射手 6輔助 for hero in hero_info_dict: # print(hero) # print(str(hero.get('cname')) + '===' + str(hero_type[hero.get('hero_type')-1]) + '===' + str(hero.get('skin_name'))) hero_type_list = [str(hero_type_dict[hero.get('hero_type') - 1])] if '|' in str(hero.get('skin_name')): skin_name_list = hero.get('skin_name').split('|') else: skin_name_list = [hero.get('skin_name')] if 'hero_type2' in str(hero): hero_type_list.append(str(hero_type_dict[hero.get('hero_type2') - 1])) # 創(chuàng)建英雄信息節(jié)點(diǎn) hero_cypher = "MERGE (n:Hero{label: '" + str(hero.get('cname')) + "'})" graph.run(hero_cypher).data() # 創(chuàng)建英雄->類(lèi)型關(guān)系 for hero_type in hero_type_list: cypher_rel = "MATCH(h:Hero{label:'" + str( hero.get('cname')) + "'}),(t:HeroType{label:'" + hero_type + "'}) MERGE (h)-[r:類(lèi)型]->(t) RETURN h,r,t" graph.run(cypher_rel).data() for skin_name in skin_name_list: # 創(chuàng)建英雄皮膚節(jié)點(diǎn) cypher = "MERGE (n:Skin{label:'" + skin_name + "'})" graph.run(cypher).data() # 創(chuàng)建英雄->皮膚關(guān)系 cypher_rel = "MATCH(h:Hero{label:'" + str( hero.get('cname')) + "'}),(s:Skin{label:'" + skin_name + "'}) MERGE (h)-[r:皮膚]->(s) RETURN h,r,s" graph.run(cypher_rel).data() print(str(hero.get('cname')) + '===' + str(hero_type_list) + '===' + str(skin_name_list))
完整代碼
import json import requests from bs4 import BeautifulSoup from py2neo import Graph, RelationshipMatcher, NodeMatcher from dict import hero_type_dict url = "bolt://localhost:7687" username = "neo4j" password = 'Suns3535' graph = Graph(url, auth=(username, password), name="wzry") node_matcher = NodeMatcher(graph=graph) relationship_matcher = RelationshipMatcher(graph=graph) def get_hero_info_list(): # 英雄的全部信息的url hero_info = 'https://pvp.qq.com/web201605/js/herolist.json' # 獲取英雄的全部信息 response = requests.get(hero_info) # 轉(zhuǎn)為字典格式 hero_info_dict = json.loads(response.text) return hero_info_dict def create_hero_type_node(): for hero_type in hero_type_dict: cypher = "MERGE (n:HeroType{label: '" + hero_type + "'})" graph.run(cypher).data() print('創(chuàng)建英雄類(lèi)型節(jié)點(diǎn)成功') def create_hero_node(): hero_info_dict = get_hero_info_list() # 1戰(zhàn)士 2法師 3坦克 4刺客 5射手 6輔助 for hero in hero_info_dict: # print(hero) # print(str(hero.get('cname')) + '===' + str(hero_type[hero.get('hero_type')-1]) + '===' + str(hero.get('skin_name'))) hero_type_list = [str(hero_type_dict[hero.get('hero_type') - 1])] if '|' in str(hero.get('skin_name')): skin_name_list = hero.get('skin_name').split('|') else: skin_name_list = [hero.get('skin_name')] if 'hero_type2' in str(hero): hero_type_list.append(str(hero_type_dict[hero.get('hero_type2') - 1])) # 創(chuàng)建英雄信息節(jié)點(diǎn) hero_cypher = "MERGE (n:Hero{label: '" + str(hero.get('cname')) + "'})" graph.run(hero_cypher).data() # 創(chuàng)建英雄->類(lèi)型關(guān)系 for hero_type in hero_type_list: cypher_rel = "MATCH(h:Hero{label:'" + str( hero.get('cname')) + "'}),(t:HeroType{label:'" + hero_type + "'}) MERGE (h)-[r:類(lèi)型]->(t) RETURN h,r,t" graph.run(cypher_rel).data() for skin_name in skin_name_list: # 創(chuàng)建英雄皮膚節(jié)點(diǎn) cypher = "MERGE (n:Skin{label:'" + skin_name + "'})" graph.run(cypher).data() # 創(chuàng)建英雄->皮膚關(guān)系 cypher_rel = "MATCH(h:Hero{label:'" + str( hero.get('cname')) + "'}),(s:Skin{label:'" + skin_name + "'}) MERGE (h)-[r:皮膚]->(s) RETURN h,r,s" graph.run(cypher_rel).data() print(str(hero.get('cname')) + '===' + str(hero_type_list) + '===' + str(skin_name_list)) # 創(chuàng)建英雄類(lèi)型節(jié)點(diǎn) create_hero_type_node() # 創(chuàng)建英雄信息 create_hero_node()
實(shí)現(xiàn)效果
到此這篇關(guān)于Python爬蟲(chóng)爬取王者榮耀英雄信息并保存到圖數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)Python爬取王者榮耀英雄信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼
這篇文章主要介紹了Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Python 通過(guò)URL打開(kāi)圖片實(shí)例詳解
這篇文章主要介紹了Python 通過(guò)URL打開(kāi)圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04