gpt4 book ai didi

python - 为什么 raw_input 失败了?

转载 作者:太空宇宙 更新时间:2023-11-03 17:17:24 26 4
gpt4 key购买 nike

我正在开发股票报价程序,该程序要求用户输入一个符号,但 raw_input 失败。我正在抓取雅虎财经。这是代码:非常感谢!!!

import urllib2
from bs4 import BeautifulSoup as bs4
symbol = raw_input("enter a stock")
def get_all_info():
i = 0
while i < len(list):
url = urllib2.urlopen("https://in.finance.yahoo.com/q?s=" + symbol[i] + "&ql=0")
soup = bs4(url,"html.parser")
for price in soup.find(attrs={'id':"yfs_l84_" + symbol[i]}):
print "real time price: " + price
i += 1
print get_all_info()

这是错误:

  traceback (most recent call last):
File "C:/Users/Raunaq/PycharmProjects/stock_market_work/ask.py", line 18, in <module>
print get_all_info()
File "C:/Users/Raunaq/PycharmProjects/stock_market_work/ask.py", line 14, in get_all_info
for price in soup.find(attrs={'id':"yfs_l84_" + name[i]}):
TypeError: 'NoneType' object is not iterable

最佳答案

据我发现,您需要输入股票名称列表并通过 BeautifulSoup 抓取雅虎财经打印出其成本,如果是这样,这里是完整的代码:

import urllib2
from bs4 import BeautifulSoup as bs4

def get_all_info():
i = 0
while i < len(list1):
url = urllib2.urlopen("https://in.finance.yahoo.com/q?s=" + list1[i] + "&ql=0")
soup = bs4(url,"html.parser")
price = soup.find(attrs={'id':"yfs_l84_" + list1[i]})
if price:
print ("real time price (for {0}): {1}".format(list1[i],price.contents[0]))
i += 1


if __name__ == "__main__":
stocks = raw_input("enter stocks names (space separated): ")
list1 = stocks.split()
get_all_info()

运行:

python script_name.py

enter stocks names (space separated): yhoo goog

real time price (for yhoo): 35.07

real time price (for goog): 728.11

关于python - 为什么 raw_input 失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com