這篇文章主要介紹如何使用Python爬取最好大學網(wǎng)大學排名,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內(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é)點是空串,屬于要判斷類型進行過濾 tds = tr("td") #等價于tr.find_all("td") univList.append([tds[0].string,tds[1].string,tds[2].string]) #NavigableString可以跨越多個層次 def printUnivList(univList,num): tplt = "{0:^6}\t{1:^10}\t{2:^6}" #:前的數(shù)字說明使用format函數(shù)的第幾個參數(shù)填充模板 print(tplt.format("排名","學校名稱","總分",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()
以上是“如何使用Python爬取最好大學網(wǎng)大學排名”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:如何使用Python爬取最好大學網(wǎng)大學排名-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://jinyejixie.com/article10/digggo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站內(nèi)鏈、App開發(fā)、云服務器、靜態(tài)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容