1. 程式人生 > >Python作業默寫和自己改編

Python作業默寫和自己改編

默寫,爬一個靜態大學排行網頁排名

from bs4 import BeautifulSoup
import bs4
import requests

def gethtmltxt(url):
    try:    
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return ""
def culi(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 printfact(ulist,num): a="{:^10}{:^6}{:^10}" print(a.format("排名","學校","地域"))
for i in range(num): u=ulist[i] print(a.format(u[0],u[1],u[2])) def main(): url="http://www.zuihaodaxue.com/zuihaodaxuepaiming2016.html" html=gethtmltxt(url) unifo=[] culi(unifo,html) printfact(unifo,20) main()

改編爬取一個類似的介面

http://gaokao.xdf.cn/201812/10838484.html

這個試試吧