这就是我到目前为止所知道的:
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_name = html_soup.select('div.profile_header')[0].text
game_length = html_soup.select('div.game_times li div')[-1].text
game_developer = html_soup.find_all('strong', string='\nDeveloper:\n')[0].next_sibling
game_publisher = html_soup.find_all('strong', string='\nPublisher:\n')[0].next_sibling
game_console = html_soup.find_all('strong', string='\nPlayable On:\n')[0].next_sibling
game_genres = html_soup.find_all('strong', string='\nGenres:\n')[0].next_sibling
print(game_name)
print(game_length)
print(game_developer)
print(game_publisher)
print(game_console)
print(game_genres)
这将输出以下内容:
God of War (2018)
31 Hours
SIE Santa Monica Studio
Sony Interactive Entertainment
PlayStation 4
Third-Person, Action, Adventure
计划用这些数据制作一个电子表格(一旦我想出了如何提取游戏名称、Main + Extras游戏长度、开发者名称、发行商、Playable On和类型字段)
所以它会存储这些数据,我认为在我存储数据之前,它应该像这样打印数据:
God of War (2018)
31 Hours
SIE Santa Monica Studio
Sony Interactive Entertainment
PlayStation 4
Third-Person, Action, Adventure
任何帮助都将不胜感激
编辑
我做了点研究,我想我需要熊猫
发布于 2018-06-10 06:17:12
如果我没理解错的话,您可以在字符串上应用strip()
来删除尾随空格。之后,您可以创建一个csv文件,将您的数据存储为df:
f = open(path_where_to_save + 'info.csv', 'a')
f.write(str(game_name)+ ',' + str(game_length) + ',' + str(game_developer))
f.close()
注意open
中的a
,它附加第一行而不是覆盖第一行
https://stackoverflow.com/questions/50777758
复制相似问题