代码缩进引起的错误
问题描述:
老师你好,我按照视频写的代码没有输出,请问是什么原因?
同学的代码
from bs4 import BeautifulSoup
info = []
with open('/home/ee/Documents/1_2code_of_video/web/new_index.html', 'r') as wb_data:
Soup = BeautifulSoup(wb_data, "lxml")
images = Soup.select('body > div.main-content > ul > li > img')
titles = Soup.select('body > div.main-content > ul > li > div.article-info > h3 > a')
descs = Soup.select('body > div.main-content > ul > li > div.article-info > p.description')
rates = Soup.select('body > div.main-content > ul > li > div.rate > span')
cates = Soup.select('body > div.main-content > ul > li > div.article-info > p.meta-info')
for image,title,desc,rate,cate in zip(images,titles,descs,rates,cates):
data = {
'image': image.get('src'),
'title': title.get_text(),
'desc' : desc.get_text(),
'rate' : rate.get_text(),
'cate' : list(cate.stripped_strings)
}
info.append(data)
for i in info:
if float(i['rate'])>3:
print(i['title'],i['cate'])
下面是运行结果:
/usr/bin/python3.5 /home/ee/Documents/1_2code_of_video/web/work2.py
Process finished with exit code 0
问题解答:
同学,你出现在缩进问题上,缩进不对,对代码逻辑就有影响了。 比如我先改改你的代码,先不判断分数是否大于3,我先看看info[ ]里面总共都有什么,如下:
from bs4 import BeautifulSoup
info = []
with open('/home/ee/Documents/1_2code_of_video/web/new_index.html', 'r') as wb_data:
Soup = BeautifulSoup(wb_data, "lxml")
images = Soup.select('body > div.main-content > ul > li > img')
titles = Soup.select('body > div.main-content > ul > li > div.article-info > h3 > a')
descs = Soup.select('body > div.main-content > ul > li > div.article-info > p.description')
rates = Soup.select('body > div.main-content > ul > li > div.rate > span')
cates = Soup.select('body > div.main-content > ul > li > div.article-info > p.meta-info')
for image,title,desc,rate,cate in zip(images,titles,descs,rates,cates):
data = {
'image': image.get('src'),
'title': title.get_text(),
'desc' : desc.get_text(),
'rate' : rate.get_text(),
'cate' : list(cate.stripped_strings)
}
info.append(data)
for i in info:
print (i['rate'])
------输出结果-------
3.0
那么现在就知道,其实info[ ]里面总共就一项数据,而且分数刚好等于3,所以按照你大于3分才输出的话,输出为空在逻辑上是正确的。
那么为什么不像视频那样输出结果呢?是因为你的info.append( data )这行代码没有放在for循环里面,也就是只是增加了数据的最后一项,正确的应该是这么写:
from bs4 import BeautifulSoup
info = []
with open('/home/ee/Documents/1_2code_of_video/web/new_index.html', 'r') as wb_data:
Soup = BeautifulSoup(wb_data, "lxml")
images = Soup.select('body > div.main-content > ul > li > img')
titles = Soup.select('body > div.main-content > ul > li > div.article-info > h3 > a')
descs = Soup.select('body > div.main-content > ul > li > div.article-info > p.description')
rates = Soup.select('body > div.main-content > ul > li > div.rate > span')
cates = Soup.select('body > div.main-content > ul > li > div.article-info > p.meta-info')
for image,title,desc,rate,cate in zip(images,titles,descs,rates,cates):
data = {
'image': image.get('src'),
'title': title.get_text(),
'desc' : desc.get_text(),
'rate' : rate.get_text(),
'cate' : list(cate.stripped_strings)
}
#问题在这里,缩进应该在for循环里面,不然没有添加每一项数据,永远都是最后一项
info.append(data)
for i in info:
if float(i['rate'])>3:
print(i['title'],i['cate'])
--------输出结果------------
Sardinia's top 10 beaches ['fun', 'Wow']
How to get tanned ['butt', 'NSFW']
How to be an Aussie beach bum ['sea']