亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python爬蟲(chóng)爬取王者榮耀英雄信息并保存到圖數(shù)據(jù)庫(kù)的操作方法

 更新時(shí)間:2024年09月28日 13:58:24   作者:叁拾舞  
本文介紹了如何使用Python爬蟲(chóng)技術(shù)從王者榮耀官方獲取英雄信息,并將數(shù)據(jù)保存到圖數(shù)據(jù)庫(kù)中,文章詳細(xì)說(shuō)明了爬取英雄名稱(chēng)、類(lèi)型及皮膚名稱(chēng)的過(guò)程,并展示了創(chuàng)建英雄類(lèi)型節(jié)點(diǎn)和英雄信息節(jié)點(diǎn)的方法

爬取信息說(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è)的示例代碼

    這篇文章主要介紹了Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python中的流程控制詳解

    Python中的流程控制詳解

    這篇文章主要介紹了Python中的流程控制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-02-02
  • Python+Opencv答題卡識(shí)別用例詳解

    Python+Opencv答題卡識(shí)別用例詳解

    這篇文章主要為大家詳細(xì)介紹了Python+Opencv答題卡識(shí)別用例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python版飛機(jī)大戰(zhàn)代碼分享

    python版飛機(jī)大戰(zhàn)代碼分享

    這篇文章主要為大家詳細(xì)介紹了python版飛機(jī)大戰(zhàn)的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Python第三方庫(kù)的安裝方法總結(jié)

    Python第三方庫(kù)的安裝方法總結(jié)

    庫(kù)library是一個(gè)泛稱(chēng),一般值作為文件形式存在的模塊以及以文件夾形式存在的包的合成,這里作了Python第三方庫(kù)的安裝方法總結(jié),包括源碼安裝、包管理器安裝以及虛擬環(huán)境相關(guān)安裝三種方式的講解
    2016-06-06
  • Python 通過(guò)URL打開(kāi)圖片實(shí)例詳解

    Python 通過(guò)URL打開(kāi)圖片實(shí)例詳解

    這篇文章主要介紹了Python 通過(guò)URL打開(kāi)圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • python模擬嗶哩嗶哩滑塊登入驗(yàn)證的實(shí)現(xiàn)

    python模擬嗶哩嗶哩滑塊登入驗(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
  • python生成圓形圖片的方法

    python生成圓形圖片的方法

    這篇文章主要為大家詳細(xì)介紹了python生成圓形圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 基于PyQt5完成pdf轉(zhuǎn)word功能

    基于PyQt5完成pdf轉(zhuǎn)word功能

    本文介紹的pdf轉(zhuǎn)word功能還有一些待完善地方,例如可增加預(yù)覽功能,實(shí)現(xiàn)每頁(yè)預(yù)覽,當(dāng)然我們可以在后續(xù)階段逐漸完善,對(duì)基于PyQt5完成的pdf轉(zhuǎn)word功能感興趣的朋友一起看看吧
    2022-06-06
  • Pytorch:Conv2d卷積前后尺寸詳解

    Pytorch:Conv2d卷積前后尺寸詳解

    這篇文章主要介紹了Pytorch:Conv2d卷積前后尺寸,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評(píng)論