[英]Python - Save data to a file
我正在嘗試將數據表保存到 txt 文件,而不是在控制台上顯示結果。 但是,我的代碼導致我在 txt 文件中沒有...有人可以幫助我嗎:
我想保存到 txt 文件的結果:
from bs4 import BeautifulSoup
import requests
import re
import json
# Input
thislist = ["AAPL"]
for symbol in thislist:
print ('Getting data for ' + symbol + '...\n')
# Set up scraper
url1 = ("http://financials.morningstar.com/finan/financials/getFinancePart.html?&callback=xxx&t=" + symbol.lower())
url2 = ("http://financials.morningstar.com/finan/financials/getKeyStatPart.html?&callback=xxx&t=" + symbol.lower())
soup1 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url1).text)[0])['componentData'], 'lxml')
soup2 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url2).text)[0])['componentData'], 'lxml')
def print_table(soup):
for i, tr in enumerate(soup.select('tr')):
row_data = [td.text for td in tr.select('td, th') if td.text]
if not row_data:
continue
if len(row_data) < 12:
row_data = ['X'] + row_data
for j, td in enumerate(row_data):
if j==0:
print('{: >30}'.format(td), end='|')
else:
print('{: ^12}'.format(td), end='|')
print()
print_table(soup1)
print()
print_table(soup2)
因此,我編寫了以下代碼將其保存到 txt 文件中,但 txt 文件中的結果為“NONE”,但結果顯示在控制台上:
from bs4 import BeautifulSoup
import requests
import re
import json
# Input
thislist = ["AAPL"]
for symbol in thislist:
print ('Getting data for ' + symbol + '...\n')
# Set up scraper
url1 = ("http://financials.morningstar.com/finan/financials/getFinancePart.html?&callback=xxx&t=" + symbol.lower())
url2 = ("http://financials.morningstar.com/finan/financials/getKeyStatPart.html?&callback=xxx&t=" + symbol.lower())
soup1 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url1).text)[0])['componentData'], 'lxml')
soup2 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url2).text)[0])['componentData'], 'lxml')
def print_table(soup):
for i, tr in enumerate(soup.select('tr')):
row_data = [td.text for td in tr.select('td, th') if td.text]
if not row_data:
continue
if len(row_data) < 12:
row_data = ['X'] + row_data
for j, td in enumerate(row_data):
if j==0:
print('{: >30}'.format(td), end='|')
else:
print('{: ^12}'.format(td), end='|')
print()
# Save the data tables to a TXT file
# Open a file with access mode 'a'
file_object = open('testing aapl data.txt', 'a')
# Append data at the end of file
file_object.write(str(print_table(soup1))+"\n")
file_object.write(str(print_table(soup2))+"\n")
# Close the file
file_object.close()
您的 function, print_table
格式化 go 上的所有內容並將所有內容傳遞到控制台,沒有任何內容被保留或返回以放入文件中。 由於print_table
不返回任何內容,因此給出了None
。
對此的解決方案可能是創建一個字符串finaltext
並將格式化后將打印在一起的所有內容連接起來,然后打印finaltext
,然后返回該字符串以供任何其他用途
以下腳本是實現的解決方案以及其他一些改進
from bs4 import BeautifulSoup
import requests
import re
import json
#Only needs to be made once, as the 'for' loop will re-create it repeated
def print_table(soup):
finaltext = ''
for i, tr in enumerate(soup.select('tr')):
row_data = [td.text for td in tr.select('td, th') if td.text]
if not row_data:
continue
if len(row_data) < 12:
row_data = ['X'] + row_data
for j, td in enumerate(row_data):
if j==0:
finaltext += '{: >30}'.format(td) + '|'
else:
finaltext += '{: ^12}'.format(td) + '|'
finaltext += '\n'
print(finaltext)
return finaltext
# Input
thislist = ["AAPL"]
for symbol in thislist:
print ('Getting data for ' + symbol + '...\n')
# Set up scraper
url1 = ("http://financials.morningstar.com/finan/financials/getFinancePart.html?&callback=xxx&t=" + symbol.lower())
url2 = ("http://financials.morningstar.com/finan/financials/getKeyStatPart.html?&callback=xxx&t=" + symbol.lower())
soup1 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url1).text)[0])['componentData'], 'lxml')
soup2 = BeautifulSoup(json.loads(re.findall(r'xxx\((.*)\)', requests.get(url2).text)[0])['componentData'], 'lxml')
# Save the data tables to a TXT file
# Open a file with access mode 'a'
file_object = open('testing_aapl_data.txt', 'a') # Habit of mine to not have spaces in filenames
# Append data at the end of file
file_object.write(str(print_table(soup1))+"\n")
file_object.write(str(print_table(soup2))+"\n")
# Close the file
file_object.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.