【问题标题】:how to write to csv from beautifulsoup data如何从 beautifulsoup 数据写入 csv
【发布时间】:2018-11-19 13:37:41
【问题描述】:

希望将我用 beautifulsoup 提取的数据转换为 .csv 文件

这是要提取的代码:

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

我想将这些结果写入 csv(它正在提取我想要的信息,但我认为它需要清理)

不确定如何写入 csv 或清理数据

请帮忙

【问题讨论】:

标签: python csv beautifulsoup


【解决方案1】:

你可以使用csv.writer:

import csv, re
from bs4 import BeautifulSoup as soup
import requests
flag = False
with open('filename.csv', 'w') as f:
  write = csv.writer(f)
  for i in range(1, 30871):
    s = soup(requests.get(f'https://howlongtobeat.com/game.php?id={i}').text, 'html.parser')
    if not flag: #write header to file once
      write.writerow(['Name', 'Length']+[re.sub('[:\n]+', '', i.find('strong').text) for i in s.find_all('div', {'class':'profile_info'})])
      flag = True
    name = s.find('div', {"class":'profile_header shadow_text'}).text
    length = [[i.find('h5').text, i.find("div").text] for i in s.find_all('li', {'class':'time_100'})]
    stats = [re.sub('\n+[\w\s]+:\n+', '', i.text) for i in s.find_all('div', {'class':'profile_info'})]
    write.writerows([[name, length[0][-1]]+stats[:4]])

【讨论】:

  • 可行,但我缺少游戏名称?不知道我会把它放在哪里
  • 完美!!!!我试图了解它是如何工作的,哈哈,但肯定完成了工作你会不会碰巧知道如何为网站howlongtobeat.com/game.php?id=(从 0 到 30870)的所有 url 编写这些数据
  • 再次感谢,但我的新手大脑再次无法解决问题我收到此错误“文件“hltb2.py”,第 12 行,在 name = s.find(' div', {"class":'profile_header shadow_text'}).text AttributeError: 'NoneType' object has no attribute 'text' 你知道为什么吗?哦,范围是从 1 到 30870,这有什么不同吗?
  • @littlejiver 是的,可能是这样 :) 请查看我最近的编辑。
【解决方案2】:

【讨论】:

    【解决方案3】:

    为了将此数据写入 CSV 文件,

    game_info = [game_name, game_publisher, game_console, game_genre, game_length, game_developer]
    with open("game.csv", 'w') as outfile:
        csv.register_dialect('custom', delimiter='\n', quoting=csv.QUOTE_NONE, escapechar='\\')
        writer = csv.writer(outfile,'custom')
        row = game_info
        writer.writerow(row)
    

    【讨论】:

    • game_info = [name, publisher, console,genre, length, developer] 中运行此“文件”hltb.py“第 18 行时出现错误 NameError: name 'name' 没有定义”你能帮忙吗?
    • name 实际上是 game_name 根据您的代码
    • 谢谢大家(对不起,我是新手)现在它给了我“NameError:名称'csv_filename'未定义”我是否必须使用该名称制作一个csv文件(我知道我可以改名)
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 2021-05-17
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多