python-Best University Rankings

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.