本文实例为大家分享了Python爬取最好大学网大学排名的具体代码,供大家参考,具体内容如下
源代码:
#-*-coding:utf-8-*-
'''''
Created on 2017年3月17日
@author: lavi
'''
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def fillUnivList(univList,html):
soup = BeautifulSoup(html,"html.parser")
for tr in soup.find("tbody").children:
if isinstance(tr,bs4.element.Tag): #tobody有的节点是空串,属于要判断类型进行过滤
tds = tr("td") #等价于tr.find_all("td")
univList.append([tds[0].string,tds[1].string,tds[2].string]) #NavigableString可以跨越多个层次
def printUnivList(univLi