#-*-Coding:utf-8-*-
"""
Created on Mon APR 3 09:37:52 2017
@author: zuihaodaxuepaiming
"""
Import BS4
Import requests
From BS4 import BeautifulSoup
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 fillunivtext (Html,uinfo):
Soup = BeautifulSoup (html, "Html.parser")
for TR in Soup.find (' tbody '). Children:
If Isinstance (tr, bs4.element.Tag):
TDS = TR (' TD ')
Uinfo.append ([Tds[0].string, Tds[1].string, tds[3].string])
def printunivtext (Uinfo,num):
# tplt = "{}\t{}\t{2:^10}"
Print ("{: ^3}\t{:^3}\t{:^3}". Format ("Rank rank", "name", "Total Score"))
For I in range (num):
U = uinfo[i]
Print ("{: ^5}\t{:^10}\t{:^10}". Format (u[0],u[1],u[2]))
def main ():
Uinfo = []
url = ("http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html")
r = Gethtmltext (URL)
Fillunivtext (R,uinfo)
Printunivtext (uinfo,20)
Main ()
--------------------
10:56:07
' {0:{3}} ' or {: ^3}
10:56:30
{0:3} can also be
10:56:56
Why start from 0?
10:57:28
Just can't {1:^3}, there is no such slice format in the Python document
10:59:03
{0:3} means that the previous tangent No. 0, 1, and 2 3*2 single-digit values?
@ big.
10:59:12
^ symbol is from the beginning, you have to make a slice from the beginning of 1 does not contradict?
10:59:14
Like a list or string of slices, just cut the whitespace into the length you want and then fill it in format (as I understand it, I don't know right.) )
sectioning, Chinese and English alignment issues
Pending solution = = Occupied pit
python-Best University Rankings