寻找我已经提取的数据与美丽的汤. 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或清理数据
请帮忙
您可以使用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]])
您可以使用Python的csv模块:https://docs.python.org/3/library/csv.html或https://docs.python.org/2.7/library/csv.html.
要将此数据写入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)