为什么raw_input失败了?

时间:2015-11-05 06:24:18

标签: python python-2.7 web-scraping yahoo-finance

我想出了股票报价程序,程序要求用户输入一个符号,但$(document).ready(function () { $("#content_ProjectName option[value='1']").prop("selected", true); }); 失败。我正在抓雅虎财务。这是代码:谢谢你!

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()

1 个答案:

答案 0 :(得分:1)

我发现你需要输入一个股票名单并打印出他们的成本,并通过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

     

输入股票名称(空格分隔):yhoo goog

     

实时价格(对于yhoo):35.07

     

实时价格(适用于goog):728.11