gpt4 book ai didi

Python BS4 Beautiful Soup HTML.Parser 无法在网站上运行

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:21 26 4
gpt4 key购买 nike

我有 python 3.7 代码来尝试从以下网站( https://www.whoscored.com/Matches/1294545/LiveStatistics/Germany-Bundesliga-2018-2019-Bayern-Munich-Hoffenheim )提取足球统计数据。看来我在 BS4 Beautiful soup 中使用的 HTML 解析器根本没有提取网站内的任何标签。

我首先尝试提取特定标签,例如代表主队和客队的两个不同的 div 标签以及包含球员姓名的标签。当呈现一个空的提取标签列表时,我只是尝试提取该网站上的所有 div 标签,但仍然得到一个空列表。

这是我使用的代码:

from requests import get
from bs4 import BeautifulSoup

url = 'https://www.whoscored.com/Matches/1294545/LiveStatistics/Germany-
Bundesliga-2018-2019-Bayern-Munich-Hoffenheim'

response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
containers_home_offensive = html_soup.find_all('div')

最佳答案

当您可以直接从 HTML 中提取比赛统计信息时,您不必使用 Selenium:

import re
from ast import literal_eval

url = 'https://www.whoscored.com/Matches/1294545/LiveStatistics/Germany-Bundesliga-2018-2019-Bayern-Munich-Hoffenheim'
res = requests.get(
url,
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
}
)
res.raise_for_status()
html = res.text

到目前为止没有什么特别的。

match_data = re.search('var matchStats = ([^;]+)', html, flags=re.MULTILINE).group(1)
match_data_clean = re.sub(',,', ",'',", match_data_clean)

stats = literal_eval(match_data_clean)

当我们检查 match_data 时,我们可以看到一堆语法无效的数组,如下所示:

ams',,'yellow',,,21,328

因此,我们通过在逗号之间插入空字符串来使用一点re魔法来清除它。

打印stats给我们:

[[[37,
1211,
'Bayern Munich',
'Hoffenheim',
'24/08/2018 19:30:00',
'24/08/2018 00:00:00',
6,
'FT',
'1 : 0',
'3 : 1',
'',
'',
'3 : 1',
'Germany',
'Germany'],
[[[21, [], [['Kasim Adams', '', 'yellow', '', '', 21, 328428, 0]], 0, 1],
[23,
[['Thomas Müller',
'Joshua Kimmich',
'goal',
'(1-0)',
'',
23,
37099,
283323]],
[],
1,
0],

从现在开始,它只是找到与您要查找的数据相对应的正确索引。

关于Python BS4 Beautiful Soup HTML.Parser 无法在网站上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57012258/

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