python - 使用 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,除非在这个阶段有办法做到这一点)
让我知道我会怎么做
我需要硒吗?
谢谢,
解决方案
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
推荐阅读
- c# - 如何在 WPF 中实现图像轮播,其中所选项目始终是第一个?
- c# - 使用 C# 描述属性和 XML 文档注释(三斜杠注释)有什么区别?
- php - Laravel 8 | 不打策略方法
- ruby - 安装瘦 (1.7.2) 时出错
- ruby-on-rails - Actioncable 使用了多少个 Redis 连接?
- java - 当应用程序被强制关闭时,AlarmManager 不起作用
- node.js - 为什么在 AWS Lambda 中创建 Stripe customer.id 失败?
- hibernate - @PostConstruct 中的 org.hibernate.LazyInitializationException
- android - SyncAdapter 中的 Firebase Crashlytics IllegalStateException
- javascript - javascript jquery,鼠标悬停事件