这是我到目前为止所做的:
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
计划使用此数据制作电子表格(一旦我弄清楚如何提取游戏名称,主要+额外游戏长度,开发者名称,出版商,可播放和流派字段)
所以它会存储这些数据,我认为它应该在我存储它之前打印这样的数据:
God of War (2018)
31 Hours
SIE Santa Monica Studio
Sony Interactive Entertainment
PlayStation 4
Third-Person, Action, Adventure
任何帮助将不胜感激
编辑---
我做了一点研究,我想我需要Pandas
答案 0 :(得分:0)
如果我理解正确,您可以删除在字符串上应用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()
请注意a
中的open
附加而不是覆盖第一行