您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Python怎么爬取軟科世界大學(xué)學(xué)術(shù)排名”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python怎么爬取軟科世界大學(xué)學(xué)術(shù)排名”吧!
Python 3.8
Spyder
#爬取軟科世界大學(xué)學(xué)術(shù)排名 import requests from bs4 import BeautifulSoup if __name__ == "__main__": destinationPath = "html信息.txt" allUniv = [] # headers={'User-Agent':'Mozilla/5.0'} url= 'https://www.shanghairanking.cn/rankings/arwu/2020' try: r = requests.get(url=url, timeout=30) r.raise_for_status() r.encoding = 'utf-8' html = r.text except: html = "" # fd = open(destinationPath,"w+") # 注意這里會報錯:UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position 0: illegal multibyte sequence fd = open(destinationPath,"w+",encoding='utf-8') fd.writelines(html) fd.close() # print(html) # 注意在vscode下這里打印不全,故將結(jié)果保存在文件中 soup = BeautifulSoup(html, "html.parser") # fillUnivList(soup) # printUnivList(10) print("{0:{5}<10}{1:{5}^10}{2:{5}^12}{3:{5}^10}{4:{5}^10}".format("排名","學(xué)校名稱","國家","排名","總分",(chr(12288)))) data = soup.find_all('tr') for tr in data: # 每一行,對應(yīng)每一個學(xué)校 ltd = tr.find_all('td') if len(ltd)==0: continue singleUniv = [] # UniName = tr.find('a') # singleUniv = [UniName.string] for td in ltd: if td.find("a"): UniName = td.find("a").string singleUniv.append(UniName) elif td.string.strip(): singleUniv.append(td.string.strip()) allUniv.append(singleUniv) # print(singleUniv) # num = len(allUniv) num = 30 for i in range(num): u=allUniv[i] print("{0:<10}{1:{5}^20}{2:{5}<10}{3:{5}^4}{4:^33}".format(u[0],u[1],u[2],u[3],u[4],(chr(12288))))#排版原因,實為一行 print("{0:{5}<10}{1:{5}^10}{2:{5}^12}{3:{5}^10}{4:{5}^10}".format("排名","學(xué)校名稱","國家","排名","總分",(chr(12288))))
到此,相信大家對“Python怎么爬取軟科世界大學(xué)學(xué)術(shù)排名”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。