0
我試圖從Yahoo Finance上的摘要頁面上刮掉「市值」數據。使用BeautifulSoup4刮取HTML表格數據
我的代碼是:
from urllib.request import urlopen
from bs4 import BeautifulSoup
sp500short = ['a', 'aa', 'aapl', 'abbv', 'abc', 'abt', 'aci', 'acn', 'act', 'adbe', 'adi', 'adm', 'adp']
dowJones = ['mmm', 'axp', 'aapl', 'ba', 'cat', 'cvx', 'csco', 'ko', 'dd', 'xom', 'ge', 'gs', 'hd', 'intc', 'ibm', 'jpm', 'jnj', 'mcd', 'mrk', 'msft', 'nke', 'pfe', 'pg', 'trv', 'utx', 'unh', 'vz', 'v', 'wmt', 'dis']
def stockScreener():
for ticker in sp500short:
searchSummary = "http://finance.yahoo.com/q?s="+ticker
summary = urlopen(searchSummary)
summaryHtml = summary.read()
summarySoup = BeautifulSoup(summaryHtml, "html.parser")
try:
marketCap = summarySoup.find("th scope", text="Market Cap:").find_next_sibling("td").text
except:
marketCap = "There is no data for this company"
if marketCap == "There is no data for this company":
print(ticker+" "+marketCap)
else:
output = marketCap[:-1]
print(ticker + str(output))
stockScreener()
有什麼不對我.find()
電話?