我希望使用 Python 脚本解析特定的雅虎股票页面(以 https://finance.yahoo.com/quote/NOA?ltr=1 为例)并将“推荐评级”打印到文件中。推荐评级可以在页面右侧大约一半的位置找到。
这就是我目前所拥有的
try:
import urllib.request as urllib2
except ImportError:
import urllib2
from bs4 import BeautifulSoup
quote_page = 'https://finance.yahoo.com/quote/NOA?ltr=1'
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, "html.parser")
name_box = soup.find(attrs={'div': 'rating-text Arrow South Fw(b) Bgc($strongBuy) Bdtc($strongBuy)'})
name = name_box.text.strip()
print(name)
棘手的部分是,我相信推荐的评级仅在页面上以 InnerHTML 形式列出。我不确定如何检索这些数据,如果能朝着正确的方向推动,我们将不胜感激!
Yahoo 向下面脚本中的 url 发出获取请求以获取其某些数据。如果您查看开发人员工具的网络选项卡并刷新 NOA 股票页面,您应该会看到“NOA?formatt...”。单击此按钮,然后查看响应对象以查看一些数据。您需要请求模块才能使以下脚本正常工作:
pip install requests
。
# get_mean_recs.py
import csv
from datetime import datetime
import requests
import sys
get_date = lambda : datetime.utcnow().strftime('%d-%m-%Y')
lhs_url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/'
rhs_url = '?formatted=true&crumb=swg7qs5y9UP&lang=en-US®ion=US&' \
'modules=upgradeDowngradeHistory,recommendationTrend,' \
'financialData,earningsHistory,earningsTrend,industryTrend&' \
'corsDomain=finance.yahoo.com'
def get_mean_rec(ticker):
url = lhs_url + ticker + rhs_url
r = requests.get(url)
if not r.ok:
return -1
result = r.json()['quoteSummary']['result'][0]
return result['financialData']['recommendationMean']['fmt']
def read_from_csv(fn):
with open(fn, 'r') as f:
reader = csv.reader(f)
for line in reader:
for ticker in line:
yield ticker
def write_to_csv(fn, data):
with open(fn, 'a') as f:
fieldnames = data[0].keys()
writer = csv.DictWriter(f, fieldnames=fieldnames)
for item in data:
writer.writerow(item)
def assemble_dict(ticker):
return {
'ticker': ticker,
'mean_rec': get_mean_rec(ticker),
'utc_date': get_date()
}
def main():
in_fn = sys.argv[1]
out_fn = sys.argv[2]
data = [assemble_dict(ticker) for ticker in read_from_csv(in_fn)]
write_to_csv(out_fn, data)
if __name__ == '__main__':
main()
用途:
python get_mean_recs.py input.csv output.csv
有一个API用于访问雅虎财经信息,例如
http://finance.yahoo.com/d/quotes.csv?s=NOA&f=snd1l1yr
我认为您最好使用它来获取所需的信息。有关参数的更多信息可以在这里找到: http://wern-ancheta.com/blog/2015/04/05/getting-started-with-the-yahoo-finance-api/
“ 棘手的部分是,我相信推荐的评级仅在页面上以 InnerHTML 形式列出。我不确定如何检索这些数据,” 我有同样的问题。你找到答案了吗?