gpt4 book ai didi

python - Beautifulsoup python3 Howlongtobeat.com 提取名称(和其他元素)

转载 作者:行者123 更新时间:2023-12-01 09:21:15 24 4
gpt4 key购买 nike

尝试弄清楚如何通过beautifulsoup提取游戏名称

我认为我在 HTML 方面遇到了问题

这是我到目前为止所拥有的:

from requests import get

url = 'https://howlongtobeat.com/game.php?id=38050'

response = get(url)

from bs4 import BeautifulSoup

html_soup = BeautifulSoup(response.text, 'html.parser')

game_length = html_soup.find_all('div', class_='game_times')

length = (game_length[-1].find_all({'li': ' short time_100 shadow_box'})[-1].contents[3].get_text())

print(length)

game_name = html_soup.find_all('div', class_='profile_header_game')

game = (game_name[].find({"profile_header shadow_text"})[].contents[].get_text())

print(game)

我得到了长度,但没有得到游戏名称,为什么?

对于打印(长度)打印:

31 Hours 

但对于打印(游戏)打印:

game_name = html_soup.find_all('div', class_='profile_header_game')

game = (game_name[].find({"profile_header shadow_text"})[].contents[].get_text()) File "", line 1 game = (game_name[].find({"profile_header shadow_text"})[].contents[].get_text()) ^ SyntaxError: invalid syntax

print(game) Traceback (most recent call last): File "", line 1, in NameError: name 'game' is not defined

我做错了什么?

最佳答案

您的代码中似乎存在一些语法问题。这是更正后的版本:

from bs4 import BeautifulSoup
import requests

url = 'https://howlongtobeat.com/game.php?id=38050'
response = requests.get(url)

html_soup = BeautifulSoup(response.text, 'html.parser')
game_times_tag = html_soup.find('div', class_='game_times')

game_time_list = []
for li_tag in game_times_tag.find_all('li'):
title = li_tag.find('h5').text.strip()
play_time = li_tag.find('div').text.strip()

game_time_list.append((title, play_time))

for game_time in game_time_list:
print(game_time)

profile_header_tag = html_soup.find("div", {"class": "profile_header shadow_text"})
game_name = profile_header_tag.text.strip()
print(game_name)

关于python - Beautifulsoup python3 Howlongtobeat.com 提取名称(和其他元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50776702/

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