Why did the if statement in the loop not work?

时间:2015-10-06 08:15:58

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

I was scraping Yahoo finance and somehow the if condition did not work. What the if was supposed to do was to print the stock price if the stock price was greater than 50, but it printed all the stocks which were above 50 and below 50.

Here is the code:

import urllib2
from bs4 import BeautifulSoup as bs4

list = ["aapl","goog","yhoo"]
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]}):
           if price > 50:
               print price
               i += 1
           else:
               print "failed"
               1 += 1

Why did it print the stock "yahoo", cause "yahoo" is less than 50, no?

3 个答案:

答案 0 :(得分:1)

We can rewrite code in following:

  1. 1 += 1 will not work because LHS should be variable name. This is i += 1 . this might be typing mistake. :)
  2. No need of i variable, we can iterate list by for loop. This will remove our i += 1 statements from the code.
  3. Do not use in-built variable names as our variable names. e.g. list is list type variable used to create new list. If we use such variable name then this will create problem in code.

e.g.

>>> list
<type 'list'>
>>> a = list()
>>> a
[]
>>> list = [1,2,3,4]
>>> a = list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> 
  1. Use exception handling during Type Casting means when we convert one data type to other data type.

Demo:

import urllib2
from bs4 import BeautifulSoup as bs4

item_list = ["aapl","goog","yhoo"]
target_url = "http://finance.yahoo.com/q?s=%s&q1=1"
target_id = "yfs_l84_%s"

for i in item_list:
    url = urllib2.urlopen(target_url%i)
    soup = bs4(url, "html.parser")
    for price in soup.find(attrs={'id':target_id%i}):
        try:
            price = float(price)
        except:
            print "Exception during type conversion. Value is %s. Type is %s."%(price, type(price)) 
            continue
        if price > 50:
            print "Price:", price
        else:
            print "failed"

答案 1 :(得分:0)

Seems to me you've mixed types:

if price > 50:

TypeError: unorderable types: NavigableString() > int()

Use this:

if float(price) > 50:
   print price

答案 2 :(得分:0)

price is string, you have to convert it to number:

   if int(price) > 50:
       print price
       i += 1
   else:
       print "failed"
       i += 1

or (with fractional part):

   if float(price) > 50:
       print price
       i += 1
   else:
       print "failed"
       i += 1
相关问题