首页 > 解决方案 > 使用 python3、漂亮的汤和硒(也许)对 howlongtobeat.com 进行数据抓取

问题描述

想知道从 howlongtobeat.com 抓取多个网址的最佳方法是什么

我正在尝试将电子表格放在一起并需要这些数据

我的想法对我们来说是 python3、beautifulsoup 和 selenium,但我不确定最好的方法

我正在使用 Linux (ubuntu 18.04) 命令控制台,可以使用一些技巧(对此非常新)

这是我到目前为止的代码:

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

response = get(url)

from bs4 import BeautifulSoup

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

game_containers = html_soup.find_all('div', class_ = 'li.short:nth-of-type(2)')

first_game = game_containers[0]
first_game.text

有一个错误显示:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

之后返回:

'\nGod of War (2018) '

我想要的是页面中的“30 1/2 小时”(理想情况下是 30.5,但我认为我可以使用 excel,除非在这个阶段有办法做到这一点)

让我知道我会怎么做

我需要硒吗?

谢谢,

标签: pythonseleniumweb-scrapingbeautifulsoup

解决方案


game_containers = soup.find_all('div', class_='game_times')

返回统计表ResultSet的a。game_times

用于[-1]获取最后一项,并获取其text

print(game_containers[-1].find_all({'li': '    short time_100 shadow_box'})[-1].contents[3].get_text())

印刷:
30½ Hours


推荐阅读