2015-10-06 86 views
0

您好我刮雅虎财经,我想打印的股票,如果大于50,但我没有反正这里工作是代码:为什么如果不工作?

import urllib2 
from bs4 import BeautifulSoup as bs4 

list = ["aapl","goog"] 
i = 0 
while i < len(list): 
     url = urllib2.urlopen("http://finance.yahoo.com/q?s="+ list[i] +"&q1=1") 
     soup = bs4(url,"html.parser") 
     for price in soup.find(attrs={'id':"yfs_l84_" + list[i]}): 
       print "something" 
       i += 1 
     if price > 200: 
      print price 
+1

您缩进事项应该会在for循环 – The6thSense

+0

不能说缩进没有看到数据,但是在这里你打印的“东西”,为什么不打印出来的价格的价值,所以你可以看到它是否有你期望的价值。 –

回答

1

你在最后两行缺少的缺口,所以它不是for循环的一部分。另外,您说大于50但代码说200

import urllib2 
from bs4 import BeautifulSoup as bs4 

list = ["aapl","goog"] 
i = 0 
while i < len(list): 
     url = urllib2.urlopen("http://finance.yahoo.com/q?s="+ list[i] +"&q1=1") 
     soup = bs4(url,"html.parser") 
     for price in soup.find(attrs={'id':"yfs_l84_" + list[i]}): 
      print "something" 
      i += 1 
      if price > 50: 
       print price 
+0

谢谢我的不好:) –

+0

@BeyondPrithvi没问题!欢迎来到SO,接受并考虑提供一个答案,如果它为你工作 – cheniel

+0

所以我添加了一个股票“YHOO”,这是30美元,但它仍然没有打破代码为什么? –