使用 BeautifulSoup 从 wiki 类别中抓取数据

哈姆扎汗

我正在尝试从https://dota2.gamepedia.com/Category:Counters.

我试过下面的代码

from bs4 import BeautifulSoup
import requests

source = requests.get('https://dota2.gamepedia.com/Category:Counters').text

soup = BeautifulSoup(source, 'lxml')
link = soup.find('div', class_="mw-category")

for link in link:
    link = link.text
    print(link)

我只需要列表形式的角色(在 DOTA2 中称为英雄)名称。随意尝试自己的代码并检查输出。

HK1911

您的代码将英雄列表作为 HeroName/Counter 给出,我基本上更正了它。hero_names 是您正在寻找的列表,我相信

from bs4 import BeautifulSoup
import requests

source = requests.get('https://dota2.gamepedia.com/Category:Counters').text

soup = BeautifulSoup(source, 'lxml')
link = soup.find('div', class_="mw-category")

heroes_names = []

for link in link:
    link = link.text
    heroes = link.split("\n")

    for i in range(1,len(heroes)):
        heroname = heroes[i].split("/")[0]

        heroes_names.append(heroname)


for hero_name in heroes_names:
    print(hero_name)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章