Get the information of all heroes in the League of Legends through the crawler.

在这里插入图片描述

源代码

from bs4 import BeautifulSoup
import re
import urllib.request, urllib.error, urllib.parse


def main():
# {
    
    
    main_url = "https://baike.baidu.com/item/%s" % urllib.parse.quote("英雄联盟") # use quote() to convert Chinese to Unicode

    html = get_html(main_url)

    soup = BeautifulSoup(html, "lxml")
    item = str(soup.find_all("table", {
    
    "data-sort": "sortDisabled"})[0]) # get content in the legend list
    all_tr = re.findall(r'<tr>(.*?)</tr>', item) # get all tr labels in the list

    for every_tr in all_tr: # traverse each tr label
        match_object = re.match(r'<td align="center" height="30" width="110">'
                                r'<div class="para" label-module="para">(.*?)</div>'
                                r'</td>'
                                r'<td align="center" height="30" width="194">'
                                r'<div class="para" label-module="para"><a data-lemmaid="(.*?)" href="(.*?)" target="_blank">(.*?)</a></div>'
                                r'</td>'
                                r'<td align="center" height="30" width="181">'
                                r'<div class="para" label-module="para">(.*?)</div>'
                                r'</td>'
                                r'<td align="center" height="30" width="148">'
                                r'<div class="para" label-module="para">(.*?)</div>'
                                r'</td>'
                                r'<td align="center" height="30" width="148">'
                                r'<div class="para" label-module="para">(.*?)</div>'
                                r'</td>'
                                r'<td align="center" height="30" width="207">'
                                r'<div class="para" label-module="para">(.*?)</div>'
                                r'</td>', every_tr)

        if match_object:
            print(match_object.group(1), end=" ") # serial number
            print(match_object.group(4), end=" ") # title
            print(match_object.group(5), end=" ") # name

            # Get the English name of the legend from a separate page.
            sub_url = "https://baike.baidu.com%s" % match_object.group(3)

            html = get_html(sub_url)
            soup = BeautifulSoup(html, "lxml")
            item = str(soup.find_all("dl", {
    
    "class": "basicInfo-block basicInfo-left"})[0])
            all_dd = re.findall(r'<dd class="basicInfo-item value">(\n[A-Z][\w\W]*?)</dd>', item) # match all the dd labels
            for every_dd in all_dd:
                match_object_en = re.match(r'\n([a-zA-Z\s\-]*)', every_dd) # match letter space and '-'
                if match_object_en is not None:
                    print(match_object_en.group(1).replace("\n", ""), end=" ") # replace the '\n' with '\0'

            print(match_object.group(6), end=" ") # ticket price
            print(match_object.group(7), end=" ") # blue essence
            print(match_object.group(8)) # join time

        else:
            match_object = re.match(r'<td align="center" width="110"><div class="para" label-module="para">(.*?)</div>'
                                    r'</td>'
                                    r'<td align="center" width="194">'
                                    r'<div class="para" label-module="para"><a data-lemmaid="(.*?)" href="(.*?)" target="_blank">(.*?)</a></div>'
                                    r'</td>'
                                    r'<td align="center" width="181"><div class="para" label-module="para">(.*?)</div>'
                                    r'</td>'
                                    r'<td align="center" width="148"><div class="para" label-module="para">(.*?)</div>'
                                    r'</td>'
                                    r'<td align="center" width="148"><div class="para" label-module="para">(.*?)</div>'
                                    r'</td>'
                                    r'<td align="center" width="207"><div class="para" label-module="para">(.*?)</div>'
                                    r'</td>', every_tr)

            if match_object:
                print(match_object.group(1), end=" ") # serial number
                print(match_object.group(4), end=" ") # title
                print(match_object.group(5), end=" ") # name

                # Get the English name of the legend from a separate page.
                sub_url = "https://baike.baidu.com%s" % match_object.group(3)
                html = get_html(sub_url)
                soup = BeautifulSoup(html, "lxml")
                item = str(soup.find_all("dl", {
    
    "class": "basicInfo-block basicInfo-left"})[0])
                all_dd = re.findall(r'<dd class="basicInfo-item value">(\n[A-Z][\w\W]*?)</dd>', item) # match all the dd labels
                for every_dd in all_dd:
                    match_object_en = re.match(r'\n([a-zA-Z\s\-]*)', every_dd)
                    if match_object_en is not None:
                        en_name = match_object_en.group(1).replace("\n", "")  # replace the '\n' with '\0'
                        print(en_name, end=" ")

                print(match_object.group(6), end=" ") # ticket price
                print(match_object.group(7), end=" ") # blue essence
                print(match_object.group(8)) # join time
# }


def get_html(url):
# {
    
    
    """
    获取页面的html文本
    """
    headers = {
    
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
                             "AppleWebKit/537.36 (KHTML, like Gecko) "
                             "Chrome/101.0.4951.54 Safari/537.36"}
    request = urllib.request.Request(url=url, headers=headers)

    # 爬取网页
    response = urllib.request.urlopen(request)
    html = response.read().decode("utf-8")

    return html
# }


if __name__ == "__main__":
# {
    
    
    main()
# }

爬取结果

由于主页面中每一行不完全相同,子页面也有区别,所以有个别英雄被遗漏。

159 炼金男爵 烈娜塔·戈拉斯克 The Chem-Baroness Renata Glasc 4500 6300 2022年2月17日
158 祖安花火 泽丽 The Spark of Zaun Zeri 4500 6300 2022年1月20日
157 愁云使者 薇古丝 The Gloomist Vex 4500 6300 2021年9月22日
156 影哨 阿克尚 4500 6300 2021年7月22日
155 灵罗娃娃 格温 Gwen 4500 6300 2021年4月15日
154 破败之王 佛耶戈 Viego 4500 6300 2021年1月21日
153 镕铁少女 芮尔 Rell 4500 6300 2020年12月10日
152 星籁歌姬 萨勒芬妮 Seraphine 4500 6300 2020年10月30日
151 沙漠玫瑰 莎弥拉 Desert Rose Samira 4500 6300 2020年9月22日
150 封魔剑魂 永恩 The Unforgotten Yone 4500 6300 2020年8月6日
149 含羞蓓蕾 莉莉娅 Lillia 4500 6300 2020年7月23日
147 残月之肃 厄斐琉斯 Aphelios 4500 6300 2019年12月12日
145 元素女皇 奇亚娜 Qiyana 4500 6300 2019年6月26日
144 魔法猫咪 悠米 Yuumi Cassandra Lee Morris 4500 6300 2019年5月14日
143 解脱者 塞拉斯 Sylas 4500 6300 2019年1月25日
142 万花通灵 妮蔻 Neeko 4500 6300 2018年12月5日
141 血港鬼影 派克 Pyke 4500 6300 2018年5月31日
136 逆羽 霞 Xayah 4500 6300 2017年4月18日
135 幻翎 洛 Rakan 4500 6300 2017年4月18日
126 河流之王 塔姆·肯奇 Tahm Kench Pat Duke 3500 4800 2015年7月9日
125 时间刺客 艾克 Ekko Antony Del Rio 4500 6300 2015年5月28日
124 星界游神 巴德 Bard 3500 4800 2015年3月12日
123 虚空遁地兽 雷克塞 Rek 3500 4800 2014年12月11日
121 沙漠皇帝 阿兹尔 Azir 3500 4800 2014年9月16日
120 迷失之牙 纳尔 Gnar Dorothy Fahn 3500 4800 2014年8月14日
119 弗雷尔卓德之心 布隆 Braum 3500 4800 2014年5月12日
118 虚空之眼 维克兹 Vel 3500 4800 2014年2月27日
117 疾风剑豪 亚索 Yasuo 4500 6300 2013年12月13日
116 暴走萝莉 金克丝 Jinx 4500 6300 2013年10月10日
115 圣枪游侠 卢锡安 Lucian 4500 6300 2013年8月22日
114 暗裔剑魔 亚托克斯 Aatrox 4500 6300 2013年6月13日
113 冰霜女巫 丽桑卓 Lissandra 3000 4800 2013年4月30日
112 生化魔人 扎克 Zac 3000 4800 2013年3月29日
111 德玛西亚之翼 奎因 Quinn 3500 4800 2013年3月1日
110 魂锁典狱长 锤石 Thresh 4500 6300 2013年1月23日
109 皮城执法官 蔚 Vi 3500 4800 2012年12月19日
108 唤潮鲛姬 娜美 Nami 2500 3150 2012年12月7日
107 影流之主 劫 Zed 4500 6300 2012年11月13日
106 蜘蛛女皇 伊莉丝 Elise 3000 4800 2012年10月25日
105 虚空掠夺者 卡兹克 Kha 4500 6300 2012年9月27日
104 暗黑元首 辛德拉 Syndra 3500 4800 2012年9月12日
103 傲之追猎者 雷恩加尔 Rengar 4500 6300 2012年8月14日
102 皎月女神 黛安娜 Diana 3500 4800 2012年8月1日
101 荆棘之兴 婕拉 Zyra 3500 4800 2012年7月19日
100 未来守护者 杰斯 Jayce 3500 4800 2012年7月7日
99 荣耀行刑官 德莱文 Draven 4500 6300 2012年6月5日
98 诺克萨斯之手 德莱厄斯 Darius 4500 6300 2012年5月23日
97 惩戒之箭 韦鲁斯 Varus 3500 4800 2012年5月1日
96 战争之影 赫卡里姆 Hecarim 3500 4800 2012年4月18日
95 仙灵女巫 璐璐 Lulu 3500 4800 2012年3月20日
94 无双剑姬 菲奥娜 Fiora Laurent 4500 6300 2012年2月29日
93 深海泰坦 诺提勒斯 Nautilus 2500 3150 2012年2月14日
92 爆破鬼才 吉格斯 Ziggs 3500 4800 2012年2月1日
91 北地之怒 瑟庄妮 Sejuani 3500 4800 2012年1月7日
90 机械先驱 维克托 Viktor 2500 3150 2011年12月13日
89 九尾妖狐 阿狸 Ahri 3500 6300 2011年12月13日
88 不灭狂雷 沃利贝尔 Volibear 3500 4800 2011年11月29日
87 潮汐海灵 菲兹 Fizz 3500 4800 2011年11月15日
86 龙血武姬 希瓦娜 Shyvana 3500 4800 2011年11月1日
85 法外狂徒 格雷福斯 Graves 4500 6300 2011年10月19日
84 远古巫灵 泽拉斯 Xerath 2500 3150 2011年10月15日
83 放逐之刃 锐雯 Riven 4500 6300 2011年9月14日
82 刀锋之影 泰隆 Talon 4500 6300 2011年8月24日
81 水晶先锋 斯卡纳 Skarner 2500 3150 2011年8月9日
80 齐天大圣 孙悟空 Wukong 4500 6300 2011年7月26日
79 曙光女神 蕾欧娜 Leona 3000 4800 2011年7月8日
78 牧魂人 约里克 Yorick 2500 3150 2011年6月22日
77 发条魔灵 奥莉安娜 Orianna Reveck 3000 6300 2011年6月1日
76 暗夜猎手 薇恩 Shauna Vayne 3000 4800 2011年5月10日
75 机械公敌 兰博 Rumble 2500 3150 2011年4月25日
74 复仇焰魂 布兰德 Brand 2000 4800 2011年4月11日
73 盲僧 李青 Lee Sin 3000 4800 2011年3月28日
72 永恒梦魇 魔腾 Nocturne 2500 3150 2011年3月15日
71 德玛西亚皇子 嘉文四世 Jarvan IV 3000 4800 2011年3月1日
70 扭曲树精 茂凯 Maokai 2500 3150 2011年2月16日
69 天启者 卡尔玛 Karma 2500 3150 2011年2月1日
68 荒漠屠夫 雷克顿 Renekton 3000 4800 2011年1月17日
67 皮城女警 凯特琳 Caitlyn Kiramman 3000 6300 2011年1月3日
66 魔蛇之拥 卡西奥佩娅 Cassiopeia Du Couteau 2500 4800 2010年12月14日
65 巨魔之王 特朗德尔 Trundle 2500 3150 2010年12月1日
64 刀锋舞者 艾瑞莉娅 Irelia 4000 6300 2010年11月15日
63 诡术妖姬 乐芙兰 LeBlanc 2500 4800 2010年11月2日
62 光辉女郎 拉克丝 Luxanna Crownguard 2500 3150 2010年10月18日
61 诺克萨斯统领 斯维因 Jericho Swain 3000 4800 2010年10月4日
60 琴瑟仙女 娑娜 Sona Buvelle 2500 3150 2010年9月21日
59 赏金猎人 厄运小姐 Miss Fortune 2500 3150 2010年9月8日
58 无畏战车 厄加特 Urgot  1000 1350 2010年8月24日
56 猩红收割者 弗拉基米尔 Vladimir 2500 3150 2010年7月27日
54 深渊巨口 克格莫 Kog 2000 4800 2010年6月24日
53 狂战士 奥拉夫 Olaf 1500 1350 2010年6月8日
52 虚空先知 玛尔扎哈 Malzahar 3000 4800 2010年5月20日
51 离群之刺 阿卡丽 Akali 2500 3150 2010年5月11日
49 狂暴之心 凯南 Kennen 2500 4800 2010年4月8日
48 暮光之眼 慎 Shen 2000 1350 2010年3月24日
47 探险家 伊泽瑞尔 Ezreal 3000 4800 2010年3月16日
46 铁铠冥魂 莫德凯撒 Mordekaiser 1500 3150 2010年2月24日
45 酒桶 古拉加斯 Gragas 2500 3150 2010年2月2日
44 不屈之枪 潘森 Pantheon 1500 3150 2010年2月2日
43 圣锤之毅 波比 Poppy 1000 450 2010年1月13日
42 狂野女猎手 奈德丽 Nidalee 3500 4800 2009年12月17日
41 兽灵行者 乌迪尔 Udyr 2500 3150 2009年12月2日
40 大发明家 黑默丁格 Cecil B 2500 3150 2009年10月14日
39 恶魔小丑 萨科 Shaco 2000 4800 2009年10月14日
38 沙漠死神 内瑟斯 Nasus 2500 3150 2009年9月30日
37 不祥之刃 卡特琳娜 Katarina Du Couteau 2500 3150 2009年9月9日
36 英勇投弹手 库奇 Corki 3500 4800 2009年9月9日
35 祖安狂人 蒙多 Dr 2000 1350 2009年8月26日
34 熔岩巨兽 墨菲特 Malphite 1000 1350 2009年8月26日
33 风暴之怒 迦娜 Janna 2000 1350 2009年8月26日
32 蒸汽机器人 布里茨 Blitzcrank 2500 3150 2009年8月26日
31 海洋之灾 普朗克 Gangplank 2500 3150 2009年8月19日
29 虚空行者 卡萨丁 Kassadin 2500 3150 2009年8月5日
28 邪恶小法师 维迦 Bob Beal 2000 1350 2009年7月24日
27 冰晶凤凰 艾尼维亚 Anivia 3500 4800 2009年7月10日
26 披甲龙龟 拉莫斯 Rammus 2500 3150 2009年7月10日
25 殇之木乃伊 阿木木 Amumu 2000 1350 2009年6月29日
24 虚空恐惧 科加斯 Cho 1500 3150 2009年6月29日
23 死亡颂唱者 卡尔萨斯 Karthus 3000 4800 2009年6月12日
22 瘟疫之源 图奇 Twitch 3000 4800 2009年5月1日
20 蛮族之王 泰达米尔 Tryndamere 3500 4800 2009年5月1日
19 时光守护者 基兰 Zilean 1000 450 2009年4月17日
18 炼金术士 辛吉德 Singed 2000 1350 2009年4月17日
17 堕落天使 莫甘娜 Morgana 2000 1350 2009年4月10日
16 武器大师 贾克斯 Jax 2500 3150 2009年4月10日
15 亡灵战神 赛恩 2000 1350 2009年4月10日
14 麦林炮手 崔丝塔娜 Tristana 1000 1350 2009年4月10日
12 无极剑圣 易 Master Yi 1000 450 2009年4月10日
11 符文法师 瑞兹 Ryze 1000 450 2009年4月10日
10 众星之子 索拉卡 Soraka 1000 450 2009年4月10日
9 雪原双子 努努 Nunu  1000 450 2009年4月10日
8 远古恐惧 费德提克 Fiddlesticks 2000 1350 2009年4月10日
7 正义天使 凯尔 Kayle  1000 450 2009年4月10日
6 迅捷斥候 提莫 Teemo 3500 6300 2009年4月10日
5 战争女神 希维尔 Sivir 1000 450 2009年4月10日
4 卡牌大师 崔斯特 Twisted Fate 3000 4800 2009年4月10日
3 牛头酋长 阿利斯塔 Alistar 1000 1350 2009年4月10日
2 寒冰射手 艾希 Ashe 1000 450 2009年4月10日
1 黑暗之女 安妮 Annie Hastur 2000 4800 2009年4月10日

附:主页面英雄列表中的部分td标签

<td align="center" height="19" width="110"><div class="para" label-module="para">148</div></td><td align="center" height="19" width="194"><div class="para" label-module="para"><a data-lemmaid="24218838" href="/item/%E8%85%95%E8%B1%AA/24218838" target="_blank">腕豪</a></div></td><td align="center" height="19" width="181"><div class="para" label-module="para">瑟提</div></td><td align="center" height="19" width="148"><div class="para" label-module="para">4500</div></td><td align="center" height="19" width="148"><div class="para" label-module="para">6300</div></td><td align="center" height="19" width="207"><div class="para" label-module="para">2020年1月16日</div></td>
<td align="center" width="110"><div class="para" label-module="para">147</div></td><td align="center" width="194"><div class="para" label-module="para"><a data-lemmaid="24168786" href="/item/%E6%AE%8B%E6%9C%88%E4%B9%8B%E8%82%83/24168786" target="_blank">残月之肃</a></div></td><td align="center" width="181"><div class="para" label-module="para">厄斐琉斯</div></td><td align="center" width="148"><div class="para" label-module="para">4500</div></td><td align="center" width="148"><div class="para" label-module="para">6300</div></td><td align="center" width="207"><div class="para" label-module="para">2019年12月12日</div></td>
<td align="center" height="19" width="110"><div class="para" label-module="para">146</div></td><td align="center" height="19" width="194"><div class="para" label-module="para"><a data-lemmaid="23814284" href="/item/%E6%B6%A4%E9%AD%82%E5%9C%A3%E6%9E%AA/23814284" target="_blank">涤魂圣枪</a></div></td><td align="center" height="19" width="181"><div class="para" label-module="para">赛娜</div></td><td align="center" height="19" width="148"><div class="para" label-module="para">4500</div></td><td align="center" height="19" width="148"><div class="para" label-module="para">6300</div></td><td align="center" height="19" width="207"><div class="para" label-module="para">2019年11月10日</div></td>
<td align="center" width="110"><div class="para" label-module="para">145</div></td><td align="center" width="194"><div class="para" label-module="para"><a data-lemmaid="23551482" href="/item/%E5%85%83%E7%B4%A0%E5%A5%B3%E7%9A%87/23551482" target="_blank">元素女皇</a></div></td><td align="center" width="181"><div class="para" label-module="para">奇亚娜</div></td><td align="center" width="148"><div class="para" label-module="para">4500</div></td><td align="center" width="148"><div class="para" label-module="para">6300</div></td><td align="center" width="207"><div class="para" label-module="para">2019年6月26日</div></td>

附:子页面中的英雄概述信息

<dl class="basicInfo-block basicInfo-left">
<dt class="basicInfo-item name" id="basic-name">中文名</dt>
<dd class="basicInfo-item value">
烈娜塔・戈拉斯克
</dd>
<dt class="basicInfo-item name" id="basic-name">外文名</dt>
<dd class="basicInfo-item value">
The Chem-Baroness Renata Glasc(美服名)<sup class="sup--normal" data-ctrmap=":11," data-sup="11">
[11]</sup><a class="sup-anchor" name="ref_[11]_35133750"> </a>
<br/>化學女爵 睿娜妲・格萊斯克(台服译名)<sup class="sup--normal" data-ctrmap=":12," data-sup="12">
[12]</sup><a class="sup-anchor" name="ref_[12]_35133750"> </a>
</dd>
<dt class="basicInfo-item name" id="basic-name">别    名</dt>
<dd class="basicInfo-item value">
炼金男爵
</dd>
<dt class="basicInfo-item name" id="basic-name">性    别</dt>
<dd class="basicInfo-item value"></dd>
<dt class="basicInfo-item name" id="basic-name">登场作品</dt>
<dd class="basicInfo-item value">
《英雄联盟》
</dd>
<dt class="basicInfo-item name" id="basic-name">所属地区</dt>
<dd class="basicInfo-item value">
<a data-lemmaid="11009600" href="/item/%E7%A5%96%E5%AE%89/11009600" target="_blank">祖安</a><sup class="sup--normal" data-ctrmap=":8," data-sup="8">
[8]</sup><a class="sup-anchor" name="ref_[8]_35133750"> </a>
</dd>
</dl>

猜你喜欢

转载自blog.csdn.net/qq_43686863/article/details/124618044
今日推荐