爬取中国大学排名网站内容

2021-03-07 21:29

阅读:647

标签:parse   bs4   exce   creat   end   ISE   created   append   parent   

# -*- coding: utf-8 -*-
"""
Created on Wed May 13 16:05:31 2020
@author: CHANYING
"""
import requests
from bs4 import BeautifulSoup
allUniv=[]
def get(url):
    try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return ""
def f(soup):
    data=soup.find_all(‘tr‘)
    fw=open("pq2015.csv","w",encoding=‘utf-8‘)
    for tr in data:
        ltd=tr.find_all(‘td‘)
        if len(ltd)==0:
            continue
        s=[]
        for td in ltd:
            s.append(td.string)
        allUniv.append(s)
        fw.write(",".join(s)+"\n")
    fw.close()
def p(num):
    print("{1:^4}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10}{6:{0}^10}{7:{0}^10}".format(chr(12288),"排名","学校名称","省市","总分","人才培养得分","科学研究得分","社会服务得分"))
    for i in range(num):
        u=allUniv[i]
        print("{1:^4}{2:{0}^11}{3:{0}^6}{4:{0}^7.1f}{5:{0}^10}{6:{0}^12}{6:{0}^10}".format(chr(12288),u[0],u[1],u[2],eval(u[3]),eval(u[4]),eval(u[5]),eval(u[6])))
def main(num):
    u=‘http://www.zuihaodaxue.cn/zuihaodaxuepaiming2015_0.html‘
    h=get(u)
    soup=BeautifulSoup(h,"html.parser")
    f(soup)
    p(num)
main(30)
   
 
 
技术图片

 技术图片

 

 

 

爬取中国大学排名网站内容

标签:parse   bs4   exce   creat   end   ISE   created   append   parent   

原文地址:https://www.cnblogs.com/TQianying/p/12883141.html


评论


亲,登录后才可以留言!