我正在尝试使用 python 和 Beautiful Soup 从雅虎财经下载损益表和资产负债表。我设法下载了两个表,但无法下载嵌套信息, 例如:在总资产中还有其他两个信息,如流动资产和非流动资产,但我的程序只下载了总资产,而不下载其他两个子语音。
def stock_scraper(ticker):
url_ce = ('https://finance.yahoo.com/quote/'+ticker+'/financials?p='+ticker)
r_ce = requests.get(url_ce)
web_content_ce = BeautifulSoup(r_ce.text, 'lxml')
url_sp = ('https://finance.yahoo.com/quote/'+ticker+'/balance-sheet?p='+ticker)
r_sp = requests.get(url_sp)
web_content_ce = BeautifulSoup(r_ce.text, 'lxml')
web_content_sp = BeautifulSoup(r_sp.text, 'lxml')
return web_content_ce, web_content_sp, ticker
def ce_preparator(web_content, ticker):
web_content = web_content.find('div', class_='D(tbrg)')
web_content = web_content.get_text(separator='\n')
file = open("ce_"+ticker+".txt", "w")
file.write(web_content)
file.close()
def sp_preparator(web_content, ticker):
web_content = web_content.find('div', class_='D(tbrg)')
web_content = web_content.get_text(separator='\n')
file = open("sp_"+ticker+".txt", "w")
file.write(web_content)
file.close()
PS:CE 表示损益表,SP 表示资产负债表