파충류 가 대학 순위 예시

8825 단어 코드 예제
# -*- coding: utf-8 -*-
"""
Created on Wed Nov  7 19:33:21 2018

#  1:              
 getHTMLText
#  2:                  
 fillUnivList()
#  3:             
  printUnivList()
"""

import requests
from bs4 import BeautifulSoup
import bs4

def getHTMLText(url):
    try:
        r = requests.get(url, timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding()
        return r.text
    except:
        return ""

def fillUnivList(ulist, html):
    soup = BeautifulSoup(html, 'html.parser')
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag): #    element.Tag     
            tds = tr('td')  # td  
            ulist.append([tds[0].string, tds[1].string, tds[2].string])

def printUnivList(ulist, num):
    "^    ,^10     ,       ,         ,    char(12288)"
    print("{:^10}\t{:^6}\t{:^10}".format("  ","   ","  "))
    tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"
    print(tplt.format("  ","   ","  ",chr(12288)))
#    print("{:^10}\t{:^6}\t{:^10}".format("  ","   ","  "))
    for i in range(num):
        u=ulist[i]
        print(tplt.format(u[0],u[1],u[2],chr(12288)))
        
    print("Suc" + str(num))

def main():
    uinfo = []
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    html = getHTMLText(url)
    fillUnivList(uinfo, html)
    printUnivList(uinfo, 20)

if '__name__'=='__main__':
    main()

좋은 웹페이지 즐겨찾기