总是提示indexerror 但是我又检查不出来哪的问题,跪求大佬解答下

0

一开始我是爬取一页的段子,当时是没问题的,后来我想爬取30页的时候就出错了

 
from bs4 import BeautifulSoup
import requests

allinone =
for page in range(34):
url='http://www.qiushibaike.com/text/page/'+str(page+2)+'/?s=4982476'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')

#获取段子内容
temp1= soup.select('.content span')
mycontent =
for i in range(len(temp1)):
mycontent.append(temp1[i].text)


#获取段子好笑数
mycount =
temp2 = soup.select('.number')
for j in range(len(temp2)):
if j % 4 == 0:
mycount.append(temp2[j].text)

#将每个段子的正文以及好笑数合成一个【正文,好笑数】加入到allinone列表里
for h in range(len(mycontent)):
temp =[mycontent[h],mycount[h]]
allinone.append(temp)
#
#通过对每个段子的好笑数来递减排列段子
def myownsort(mylist):
for k in range(len(allinone)-1):
for m in range(len(allinone)-1):
if int(allinone[m+1][1]) > int(allinone[m][1]):
temp = allinone[m]
allinone[m] = allinone[m+1]
allinone[m+1] = temp



#打印段子
myownsort(allinone)
for n in range(len(allinone)):
print('%s \n 好笑数为 : %s' % (allinone[n][0], allinone[n][1]))



[/i]
QQ截图20170515124600.png
已邀请:
0

ID王大伟 - 人生苦短,我选Python。 2017-05-16 回答

加异常处理try 
这个报错经常是爬的内容为空
可以设置爬一下打印一下爬取内容检查

要回复问题请先登录注册