python爬⾍如何解决listindexoutofrange的问题
在爬取⼤学排名时,⼀直出现list index out of range的错误,求⼤佬解答
import requests
import bs4
from bs4 import BeautifulSoup
def getHTMLText(url):
try:
(url,timeout=30)
r.raise_for_status()
except:
return "出现异常"
def fillUnivList(ulist,html):
soup=BeautifulSoup(html,"html.parser")
for tr in soup.find('body').children:
if isinstance(tr,bs4.element.Tag):
tds=tr('td')
ulist.append([tds[0].string,tds[1].string,tds[2].string])
def printUnivList(ulist,num):
get out ofprint ("{:^10}\t{:^6}\t{:^10}".format("排名","学校名称","地区"))
for i in range(num):
u=ulist[i]
print("{:^10}\t{:^6}\t{:^10}".format(u[0],u[1],u[2]))
def main():
uinfo=[]
url='baike.baidu/item/QS%E4%B8%96%E7%95%8C%E5%A4%A7%E5%AD%A6%E6%8E%92%E5%90%8D/3292552#6_1'    html=getHTMLText(url)
fillUnivList(uinfo,html)
printUnivList(uinfo,20)
main()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。