我正在制作一个股票筛选器,该筛选器可从Yahoo Finance获得技术支持。 该程序将创建一个字典,其中包含统计页面上显示的所有技术信息。我有点困惑,因为它没有'Price / Book'值,即使它采用与包括的其他技术一样的html格式,例如'Eturn on Equity'。
我有点困惑,因为它没有'Price / Book'值,即使它采用与其他技术相同的html格式,例如'Eturn on Equity'。
def scrape_yahoo(stock):
technicals = {}
try:
url = ('http://finance.yahoo.com/q/ks?s='+stock)
page = urllib2.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
tables = soup.findAll('table', {"class" : 'table-qsp-stats'}) # Found using page inspection
for table in tables:
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
col_name = row.find_all('span') # Use span to avoid supscripts
col_name = [cell.text.strip() for cell in col_name]
col_val = row.find_all('td')
col_val = [cell.text.strip() for cell in col_val]
technicals[col_name[0]] = col_val[1] # col_val[0] is the name cell (with subscript)
return technicals
except Exception as e:
print('Failed, exception: ', str(e))
def scrape(stock_list, interested, technicals):
SuggestedStocks = []
for each_stock in stock_list:
technicals = scrape_yahoo(each_stock)
condition_1 = float(technicals.get('Return on Equity',0).replace('%','').replace('N/A','-100')) > 25
condition_2 = float(technicals.get('Trailing P/E',0).replace('N/A','0')) < 25
condition_3 = float(technicals.get('Price/Book(mrq)',0)) <8
condition_4 = float(technicals.get('Beta (3Y Monthly)',0)) <1.1
if condition_1 and condition_2 and condition_3 and condition_4:
print(each_stock)
SuggestedStocks.append(each_stock)
for ind in interested:
print(ind + ": "+ technicals[ind])
print("------")
time.sleep(1)
# Use delay to avoid getting flagged as bot
#return technicals
print(SuggestedStocks)
def main():
stock_list = ['MMM', 'ABT', 'ABBV', 'ABMD', 'ACN', 'ATVI', 'ADBE', 'AMD', 'AAP', 'AES', 'AMG', 'AFL', 'A', 'APD', 'AKAM', 'ALK', 'ALB', 'ARE', 'ALXN', 'ALGN', 'ALLE', 'AGN', 'ADS', 'LNT', 'ALL', 'GOOGL', 'GOOG', 'MO', 'AMZN', 'AMCR', 'AEE', 'AAL', 'AEP', 'AXP', 'AIG']
interested = ['Return on Equity', 'Revenue', 'Quarterly Revenue Growth','Trailing P/E', 'Beta (3Y Monthly)']#,'Price/Book(mrq)']
technicals = {}
tech = scrape(stock_list, interested, technicals)
print(tech)
main()
KeyError:'Price / Book(mrq)',我理解此错误的含义,我的主要问题是为什么不将此值添加到字典中?
答案 0 :(得分:1)
页面上该字段的名称是“价格/书(mrq)”而不是“价格/书(mrq)”,您错过了一个空格。