# -*- 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("排名排名","名称","总分"))
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}}‘ 或者 {:^3}
10:56:30
{0:3}也可以
10:56:56
为什么要从0开始?
10:57:28
就是不能{1:^3} , python文档里面没有这种切片格式
10:59:03
{0:3}意思是接着前面的切取第0、1、2个共3*2个位值是吗?
@大大的
10:59:12
^符号就是从头开始,你又制定切片从1开始不矛盾吗?
10:59:14
类似于列表或字符串的切片,只是切空格符切成你想要的长度再通过format填进去(我是这么理解的,不知道对不对。)
切片、中英文对齐问题
待解决==占坑