我才开始在 python 编码大约一个月。我可以覆盖以前解析过的数据吗?正文继续提问
Ive only started coding in python for about a month. Can I overwrite previously parsed data? Question continued in the body
我编写了一个函数,可以在从雅虎金融解析后收集特定股票的信息。现在在函数的末尾,我会让用户选择搜索另一个报价或返回主页。当用户单击此选项时,程序崩溃,提示数组的长度必须相同。我假设因为从前面的引用中收集的数据已经在函数中注册,并且不会允许用户覆盖和解析。我将如何解决这个问题?请指教。
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")
指标=
{"Previous Close":[],"Open":[],"Bid":[],"Ask":[],'Volume':[] , 'Avg. Volume': [], 'Market Cap': [], 'Beta': [], 'PE Ratio (TTM)': [], 'EPS (TTM)': [], 'Earnings Date': [], 'Ex-Dividend Date': [], '1y Target Est' : []}
杂项 =
{'52 周范围':[],"Day's Range":[],'Dividend & Yield':[]}
在软件中发现的问题是行:
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 循环而不是函数调用自身
指标和杂项——内部函数,因此它们会针对处理的每只股票进行重置
main 调用 tickersymbol,完成后 returns 调用它
修改后的代码
import random
import requests
import numpy as np
import pandas as pd
def main():
tickersymbol()
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 # continues with the outer while loop
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 # continues with outer while loop
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 # continues with outer while loop
elif task == "2":
return # done with tickersymbol function
else:
print("Try to answer that one again")
if __name__ == "__main__":
main()
我编写了一个函数,可以在从雅虎金融解析后收集特定股票的信息。现在在函数的末尾,我会让用户选择搜索另一个报价或返回主页。当用户单击此选项时,程序崩溃,提示数组的长度必须相同。我假设因为从前面的引用中收集的数据已经在函数中注册,并且不会允许用户覆盖和解析。我将如何解决这个问题?请指教。
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")
指标=
{"Previous Close":[],"Open":[],"Bid":[],"Ask":[],'Volume':[] , 'Avg. Volume': [], 'Market Cap': [], 'Beta': [], 'PE Ratio (TTM)': [], 'EPS (TTM)': [], 'Earnings Date': [], 'Ex-Dividend Date': [], '1y Target Est' : []}
杂项 =
{'52 周范围':[],"Day's Range":[],'Dividend & Yield':[]}
在软件中发现的问题是行:
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 循环而不是函数调用自身
指标和杂项——内部函数,因此它们会针对处理的每只股票进行重置
main 调用 tickersymbol,完成后 returns 调用它
修改后的代码
import random
import requests
import numpy as np
import pandas as pd
def main():
tickersymbol()
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 # continues with the outer while loop
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 # continues with outer while loop
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 # continues with outer while loop
elif task == "2":
return # done with tickersymbol function
else:
print("Try to answer that one again")
if __name__ == "__main__":
main()