Python 网页爬虫遇到赋值为空 超出范围

代码如下: #e23.1CrawUnivRanking.py import requests from bs4 import BeautifulSoup allUniv = [] def getHTMLText(url):     try:         r = requests.get(url, timeout=30)         r.raise_for_status()         r.encoding = 'utf-8'         return r.text     except:         return "" def fillUnivList(soup):     data = soup.find_all('tr')     for tr in data:         ltd = tr.find_all('td')         if len(ltd)==0:             continue         singleUniv = []         for td in ltd:             singleUniv.append(td.string)         allUniv.append(singleUniv) def printUnivList(num):     print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))     for i in range(num):         u=allUniv[i]         print("{:^4}{:^10}{:^5}{:^8}{:^10}".format(u[0],u[1],u[2],u[3],u[6])) def main():     url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'     html = getHTMLText(url)     soup = BeautifulSoup(html, "html.parser")     fillUnivList(soup)     printUnivList(10) main() 运行出现:list index out of range怎么解决?

查看全部
weixin_46708292
weixin_46708292
2020/12/02 19:48
  • python
  • 点赞
  • 收藏
  • 回答
    私信

5个回复