亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Python爬取最好大學(xué)網(wǎng)大學(xué)排名

 更新時(shí)間:2018年02月24日 08:31:46   作者:Lavi_qq_2910138025  
這篇文章主要介紹了如何使用Python爬取最好大學(xué)網(wǎng)大學(xué)排名,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python爬取最好大學(xué)網(wǎng)大學(xué)排名的具體代碼,供大家參考,具體內(nèi)容如下

源代碼:

#-*-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有的節(jié)點(diǎn)是空串,屬于要判斷類型進(jìn)行過濾 
      tds = tr("td") #等價(jià)于tr.find_all("td") 
      univList.append([tds[0].string,tds[1].string,tds[2].string]) #NavigableString可以跨越多個(gè)層次 
 
def printUnivList(univList,num): 
  tplt = "{0:^6}\t{1:^10}\t{2:^6}" #:前的數(shù)字說明使用format函數(shù)的第幾個(gè)參數(shù)填充模板 
  print(tplt.format("排名","學(xué)校名稱","總分",chr(12288))) 
  for i in range(num): 
    u = univList[i] 
    print(tplt.format(u[0],u[1],u[2],chr(12288))) 
def main(): 
  url= "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html"; 
  html = getHTMLText(url) 
  univList=[] 
  fillUnivList(univList,html) 
  printUnivList(univList,20) 
   
main() 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論