大学排名
Posted marcoreus11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大学排名相关的知识,希望对你有一定的参考价值。
import requests import pandas as pd import numpy as np from bs4 import BeautifulSoup import sqlite3 allUniv=[] def gethtmlText(url): try: r=requests.get(url,timeout=30) r.raise_for_status() r.encoding = ‘utf-8‘ return r.text except: return "error" 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): with open(r‘D:mypython大学排名30.CSV‘,‘w‘) as f: f.write("{1:^2}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10} ".format((chr(12288)),"排名","学校名称","省市","总分","科研服务")) for i in range(num): u=allUniv[i] f.write("{1:^2}{2:{0}^10}{3:{0}^6}{4:{0}^8.1f}{5:{0}^10} ".format((chr(12288)),i+1,u[1],u[2],eval(u[3]),u[11])) f.close() if 1: print("successful") else: print("fail") def main(num): url=‘http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html‘ html = getHTMLText(url) soup = BeautifulSoup(html,"html.parser") fillUnivList(soup) printUnivList(num) main(500)
以上是关于大学排名的主要内容,如果未能解决你的问题,请参考以下文章