python爬取大学排名

返回加载出来的数据
def getHTMLText(url):
	return ""
解析列表
def fillUnivList(ulist, html):
	pass
打印数据
def printUnivList(ulist, num):
	pass
进行调用函数,返回结果
def main():
    unifo = []
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    # 加载html
    html = getHTMLText(url)
    # 解析html,返回列表
    fillUnivList(unifo, html)
    printUnivList(unifo, 20)

main()

getHTMLText函数代码如下:

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

fillUnivList代码如下, 过滤不是tr的标签,找到tr下的td下的三行

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

printUnivList代码如下,遍历前二十列数据,汉字是另一种编码

def printUnivList(ulist, num):
    # 位置,补数字,约束几个字符
    tplt = "{0:^5}\t{1:{3}^10}\t{2:^4}"
    print(tplt.format("排名", "学校", "总分", chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0], u[1], u[2], chr(12288)))

整体代码如下:

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 fillUnivList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children:
        # 过滤
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')
            ulist.append([tds[0].string, tds[1].string, tds[2].string])


def printUnivList(ulist, num):
    # 位置,补数字,约束几个字符
    tplt = "{0:^5}\t{1:{3}^10}\t{2:^4}"
    print(tplt.format("排名", "学校", "总分", chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0], u[1], u[2], chr(12288)))


def main():
    unifo = []
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    # 加载html
    html = getHTMLText(url)
    # 解析html,返回列表
    fillUnivList(unifo, html)

    printUnivList(unifo, 20)


main()

你可能感兴趣的:(python)