【问题标题】:Beautifulsoup python Howlongtobeat.com extracting elements and export to .csvBeautifulsoup python Howlongtobeat.com 提取元素并导出到 .csv
【发布时间】:2018-06-09 19:24:44
【问题描述】:

这是我目前所拥有的:

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

【问题讨论】:

    标签: python html pandas web-scraping beautifulsoup


    【解决方案1】:

    如果我理解正确,您可以删除在字符串上应用 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 追加而不是覆盖第一行

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 2020-09-05
      • 2014-03-07
      • 2021-12-19
      • 2017-02-20
      • 2021-03-23
      • 1970-01-01
      • 2016-08-08
      • 2013-05-25
      相关资源
      最近更新 更多