i am trying to convert all my steam game-hours to pandas dataframe, but i am getting output like below, all games and hours looks like same. is there any mistake that i made in the code if so, how can i fix it?
output:
title hour
0 The Witcher 3: Wild Hunt kayıtlarda 137 saat
1 The Witcher 3: Wild Hunt kayıtlarda 137 saat
2 The Witcher 3: Wild Hunt kayıtlarda 137 saat
3 The Witcher 3: Wild Hunt kayıtlarda 137 saat
4 The Witcher 3: Wild Hunt kayıtlarda 137 saat
.. ... ...
312 The Witcher 3: Wild Hunt kayıtlarda 137 saat
313 The Witcher 3: Wild Hunt kayıtlarda 137 saat
314 The Witcher 3: Wild Hunt kayıtlarda 137 saat
315 The Witcher 3: Wild Hunt kayıtlarda 137 saat
316 The Witcher 3: Wild Hunt kayıtlarda 137 saat
[317 rows x 2 columns]
from selenium import webdriver
import pandas as pd
url = 'https://steamcommunity.com/id/username/games/?tab=all'
driver = webdriver.Chrome(
executable_path="path/chromedriver.exe")
driver.get(url)
game_hour_infos = []
games = driver.find_elements_by_class_name('gameListRow')
for game in games:
title = game.find_element_by_xpath(
'//*[starts-with(@id,"game_")]/div[2]/div[1]/div[1]/div').text
hour = game.find_element_by_xpath(
'//*[starts-with(@id,"game_")]/div[2]/div[1]/div[1]/h5').text
ghi = {
"title": title,
"hour": hour
}
game_hour_infos.append(ghi)
print(pd.DataFrame(game_hour_infos))
driver.close()
CodePudding user response:
Since you are trying to find element within element, put a .
at the beginning of the xpath. Try like below:
games = driver.find_elements_by_class_name("gameListRow")
for game in games:
title = game.find_element_by_xpath('.//div[contains(@class,"gameListRowItemName")]').text
hour = game.find_element_by_xpath('.//h5[contains(@class,"hours_played")]').text
print(f"{title} : {hour}")