我编写了一个函数,可以从Yahoo Finance解析特定股票后收集信息。现在,在函数结尾处,我将为用户提供搜索其他报价或返回首页的选项。当用户单击此选项时,程序将崩溃,并说数组必须全部具有相同的长度。我假设由于从前一个引号中收集的数据已经在函数中注册,因此不会允许用户覆盖和解析。我该如何解决这个问题?请指教。
import random
import requests
import numpy as np
import pandas as pd
def tickersymbol():
tickersymbol = input("What company would you like information on?")
url = ('https://ca.finance.yahoo.com/quote/'+tickersymbol+'?p='+tickersymbol+'&.tsrc=fin-srch')
response = requests.get(url)
htmltext = response.text
for indicator in Indicators :
splitlist = htmltext.split(indicator)
afterfirstsplit =splitlist[1].split("\">")[2]
aftersecondsplit = afterfirstsplit.split("</span>")
datavalue = aftersecondsplit[0]
Indicators[indicator].append(datavalue)
for values in Misc:
splitlist = htmltext.split(values)
afterfirstsplit =splitlist[1].split("\">")[1]
aftersecondsplit = afterfirstsplit.split("</td>")
netset = aftersecondsplit[0]
Misc[values].append(netset)
Indicators.update(Misc)
df = pd.DataFrame(Indicators)
array = np.transpose(df)
print(array)
task = input('''
Would you like to continue?
[1] : Yes, Look at another Symbol ##Here is where the problem starts.
[2] : No, Go back to main
''')
if task == "1":
return tickersymbol()
elif task == "2":
return main()
else:
print("Try to answer that one again")
指标=
{“上一个收盘价”:[],“开盘价”:[],“出价”:[],“询问”:[],“交易量”:[],“平均”。交易量”:[],“市值”:[],“测试版”:[],“市盈率(TTM)”:[],“每股收益(TTM)”:[],“收入日期”:[], “除息日”:[],“ 1年目标估算值”:[]}
其他=
{'“ 52周范围”:[],“日范围”:[],“股息和收益率”:[]}
发现与软件有关的问题是一行:
tickersymbol = input("What company would you like information on?")
因为这与函数名称相同,所以当您尝试再次调用该函数时,这会导致错误,即
return tickersymbol()
解决方法,将行更改为:
tickersymbol_ = input("What company would you like information on?")
url = ('https://ca.finance.yahoo.com/quote/'+tickersymbol_+'?p='+tickersymbol_+'&.tsrc=fin-srch')
其他修订
错误检查-使用try / except块
控制流-使用while循环而不是函数调用自身
指标和其他-内部函数,因此将为每个处理的库存重置它们>]
修订后的代码
import random
import requests
import numpy as np
import pandas as pd
def main():
pass
def tickersymbol():
while True:
skip = False
tickersymbol_ = input("What company would you like information on?")
url = ('https://ca.finance.yahoo.com/quote/'+tickersymbol_+'?p='+tickersymbol_+'&.tsrc=fin-srch')
response = requests.get(url)
htmltext = response.text
# Reset Indicator and Misc
Indicators = {"Previous Close" : [], "Open" : [], "Bid" : [] , "Ask": [], 'Volume': [], 'Avg. Volume': [], 'Market Cap': [], 'Beta': [], 'PE Ratio (TTM)': [], 'EPS (TTM)': [], 'Earnings Date': [], 'Ex-Dividend Date': [], '1y Target Est' : []}
Misc = {'52 Week Range' :[], "Day's Range": [], 'Dividend & Yield' : []}
for indicator in Indicators :
try:
splitlist = htmltext.split(indicator)
afterfirstsplit =splitlist[1].split("\">")[2]
aftersecondsplit = afterfirstsplit.split("</span>")
datavalue = aftersecondsplit[0]
Indicators[indicator].append(datavalue)
except:
print('Error with stock')
skip = True
break
if skip:
continue
for values in Misc:
try:
splitlist = htmltext.split(values)
afterfirstsplit =splitlist[1].split("\">")[1]
aftersecondsplit = afterfirstsplit.split("</td>")
netset = aftersecondsplit[0]
Misc[values].append(netset)
except:
print('Error with stock')
skip = True
break
if skip:
continue
Indicators.update(Misc)
df = pd.DataFrame(Indicators)
array = np.transpose(df)
print(array)
task = input('''
Would you like to continue?
[1] : Yes, Look at another Symbol ##Here is where the problem starts.
[2] : No, Go back to main
''')
if task == "1":
continue
elif task == "2":
break
else:
print("Try to answer that one again")
tickersymbol()