【问题标题】:Data Scraping howlongtobeat.com with python3, beautiful soup, and selenium(maybe)使用 python3、漂亮的汤和硒(也许)对 howlongtobeat.com 进行数据抓取
【发布时间】:2018-06-09 00:39:59
【问题描述】:

想知道从 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,除非在此阶段有办法做到这一点)

让我知道我会怎么做

我需要硒吗?

谢谢,

【问题讨论】:

  • 由于你想要的页面信息没有被 javascript 加载,你不应该需要 selenium

标签: python selenium web-scraping beautifulsoup


【解决方案1】:
game_containers = soup.find_all('div', class_='game_times')

返回game_times 统计表的ResultSet

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

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

打印:
30½ Hours

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2018-10-19
    • 2016-04-20
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2021-11-27
    相关资源
    最近更新 更多