gpt4 book ai didi

python - 使用 BeautifulSoup4 解析数据

转载 作者:行者123 更新时间:2023-11-28 22:32:59 24 4
gpt4 key购买 nike

import requests
from bs4 import BeautifulSoup

request = requests.get("http://www.lolesports.com/en_US/worlds/world_championship_2016/standings/default")
content = request.content
soup = BeautifulSoup(content, "html.parser")
team_name = soup.findAll('text', {'class': 'team-name'})

print(team_name)

我正在尝试从 url:"http://www.lolesports.com/en_US/worlds/world_championship_2016/standings/default "解析数据。在 <text class="team-name">SK Telecom T1</text> 下是各个团队的名称。我想做的是解析该数据 (SK Telecom T1) 并将其打印到屏幕上,但我得到 [] 一个空列表。我做错了什么?

最佳答案

您不需要 selenium,所有动态内容都可以通过对 http://api.lolesports.com/api/v1/leagues 的简单获取请求以 json 格式检索:

import requests

data = requests.get("http://api.lolesports.com/api/v1/leagues?slug=worlds").json()

这给了你一大堆数据,你想要的似乎都在 data["teams"] 下。其中的一个片段是:

[{'id': 2, 'slug': 'bangkok-titans', 'name': 'Bangkok Titans', 'teamPhotoUrl': 'http://na.lolesports.com/sites/default/files/BKT_GPL.TMPROFILE_0.png', 'logoUrl': 'http://assets.lolesports.com/team/bangkok-titans-597g0x1v.png', 'acronym': 'BKT', 'homeLeague': 'urn:rg:lolesports:global:league:league:12', 'altLogoUrl': None, 'createdAt': '2014-07-17T18:34:47.000Z', 'updatedAt': '2015-09-29T16:09:36.000Z', 'bios': {'en_US': 'The Bangkok Titans are the undisputed champions of Thailand’s League of Legends esports scene. They achieved six consecutive 1st place finishes in the Thailand Pro League from 2014 to 2015. However, they aren’t content with just domestic domination.

如果命令:

,每个团队都列在列表中:

In [1]: import requests


In [2]: data = requests.get("http://api.lolesports.com/api/v1/leagues?slug=worlds").json()


In [3]: for d in data["teams"]:
...: print(d["name"])
...:
Bangkok Titans
ahq e-Sports Club
SK Telecom T1
TSM
Fnatic
Cloud9
Counter Logic Gaming
H2K
Edward Gaming
INTZ e-Sports
paiN Gaming
Origen
LGD Gaming
Invictus Gaming
Royal Never Give Up
Flash Wolves
Splyce
Samsung Galaxy
KT Rolster
ROX Tigers
G2 Esports
I May
Albus NoX Luna

关于python - 使用 BeautifulSoup4 解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40390304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com