为什么循环中的 if 语句不起作用?
Why did the if statement in the loop not work?
我正在抓取雅虎财务,但不知何故 if
条件不起作用。 if
应该做的是如果股票价格大于 50 就打印股票价格,但它打印了所有高于 50 和低于 50 的股票。
代码如下:
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
为什么打印股票 "yahoo",因为 "yahoo" 小于 50,不是吗?
在我看来你是混合类型:
if price > 50:
TypeError: unorderable types: NavigableString() > int()
使用这个:
if float(price) > 50:
print price
price
是字符串,需要转成数字:
if int(price) > 50:
print price
i += 1
else:
print "failed"
i += 1
或(带小数部分):
if float(price) > 50:
print price
i += 1
else:
print "failed"
i += 1
我们可以重写代码如下:
1 += 1
将不起作用,因为 LHS 应该是变量名。这是 i += 1
。这可能是打字错误。 :)
- 不需要
i
变量,我们可以通过for
循环迭代list
。这将从代码中删除我们的 i += 1
语句。
- 不要使用内置变量名作为我们的变量名。例如
list
是用于创建新列表的 list type
变量。如果我们使用这样的变量名,那么这将在代码中产生问题。
例如.
>>> 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
>>>
- 在类型转换期间使用异常处理意味着当我们将一种数据类型转换为另一种数据类型时。
演示:
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"
我正在抓取雅虎财务,但不知何故 if
条件不起作用。 if
应该做的是如果股票价格大于 50 就打印股票价格,但它打印了所有高于 50 和低于 50 的股票。
代码如下:
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
为什么打印股票 "yahoo",因为 "yahoo" 小于 50,不是吗?
在我看来你是混合类型:
if price > 50:
TypeError: unorderable types: NavigableString() > int()
使用这个:
if float(price) > 50:
print price
price
是字符串,需要转成数字:
if int(price) > 50:
print price
i += 1
else:
print "failed"
i += 1
或(带小数部分):
if float(price) > 50:
print price
i += 1
else:
print "failed"
i += 1
我们可以重写代码如下:
1 += 1
将不起作用,因为 LHS 应该是变量名。这是i += 1
。这可能是打字错误。 :)- 不需要
i
变量,我们可以通过for
循环迭代list
。这将从代码中删除我们的i += 1
语句。 - 不要使用内置变量名作为我们的变量名。例如
list
是用于创建新列表的list type
变量。如果我们使用这样的变量名,那么这将在代码中产生问题。
例如.
>>> 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
>>>
- 在类型转换期间使用异常处理意味着当我们将一种数据类型转换为另一种数据类型时。
演示:
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"