我希望使用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。我不确定如何检索这些数据,我们将非常感谢正确的推动!
答案 0 :(得分:2)
雅虎在下面的脚本中为其部分数据发出了获取请求。如果您查看开发者工具的网络选项卡并刷新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
答案 1 :(得分:0)
有一个用于访问雅虎财务信息的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/