首页 > 其他 > 详细

爬取中国大学排名(爬取年费)

时间:2020-05-13 21:35:38      阅读:80      评论:0      收藏:0      [点我收藏+]

爬取中国大学网站http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html

代码:

import requests
from bs4 import BeautifulSoup
allUniv = []
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = utf-8
        return r.text
    except:
        return ""
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):
    print("{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]
        print("{1:^4}{2:{0}^10}{3:{0}^5}{4:{0}^8.1f}{5:{0}^11}".format(chr(12288),u[0],u[1],u[2],eval(u[3]),u[11]))
def main():
    url = http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillUnivList(soup)
    printUnivList(10)
main()

结果:

技术分享图片

爬取中国大学排名(爬取年费)

原文:https://www.cnblogs.com/jiana/p/12884465.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!