首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.csv python Howlongtobeat.com提取元素并导出到Beautifulsoup

.csv python Howlongtobeat.com提取元素并导出到Beautifulsoup
EN

Stack Overflow用户
提问于 2018-06-10 03:24:44
回答 1查看 559关注 0票数 0

这就是我到目前为止所知道的:

代码语言:javascript
复制
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)

这将输出以下内容:

代码语言:javascript
复制
God of War (2018) 
31 Hours 

SIE Santa Monica Studio 

Sony Interactive Entertainment 

PlayStation 4 

Third-Person, Action, Adventure 

计划用这些数据制作一个电子表格(一旦我想出了如何提取游戏名称、Main + Extras游戏长度、开发者名称、发行商、Playable On和类型字段)

所以它会存储这些数据,我认为在我存储数据之前,它应该像这样打印数据:

代码语言:javascript
复制
God of War (2018) 
31 Hours 
SIE Santa Monica Studio
Sony Interactive Entertainment
PlayStation 4
Third-Person, Action, Adventure

任何帮助都将不胜感激

编辑

我做了点研究,我想我需要熊猫

EN

回答 1

Stack Overflow用户

发布于 2018-06-10 06:17:12

如果我没理解错的话,您可以在字符串上应用strip()来删除尾随空格。之后,您可以创建一个csv文件,将您的数据存储为df:

代码语言:javascript
复制
f = open(path_where_to_save + 'info.csv', 'a')
f.write(str(game_name)+ ',' + str(game_length) + ',' + str(game_developer))
f.close()

注意open中的a,它附加第一行而不是覆盖第一行

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50777758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档