这段代码似乎工作正常,控制台向我提供了我所需要的,没有任何错误。但是,当我尝试使用excel打开它时,却给了我一个空文件。
import csv
import urllib.request
from bs4 import BeautifulSoup
f = open('aapl_analyst_estimates', 'w', newline= '')
writer = csv.writer(f)
soup = BeautifulSoup(urllib.request.urlopen('https://www.marketwatch.com/investing/stock/aapl/analystestimates').read(), 'lxml')
tbody = soup('table', {'class':'snapshot'})[0].find_all('tr')
for row in tbody:
cols = row.findChildren(recursive=False)
cols = [ele.text.strip() for ele in cols]
writer.writerow(cols)
print(cols)
f.close()
答案 0 :(得分:1)
对我来说很好。我得到以下输出:
['Number of Ratings:', '41', 'Current Quarters Estimate:', '4.17']
['FY Report Date:', '9 / 2019', "Current Year's Estimate:", '11.99']
["Last Quarter's Earnings:", '2.91', 'Median PE on CY Estimate:', '12.88']
['Year Ago Earnings:', '11.75', 'Next Fiscal Year Estimate:', '13.34']
['', '', 'Median PE on Next FY Estimate:', '11.37']