课堂练习-爬网(2)-爬网代码

爬网代码:

import requests
from bs4 import BeautifulSoup
allStudents = []

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

def fillStudentsList(soup):
    data = soup.select('.exesoft-table tr')
    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd)==0:
            continue
        singleStudent = []
        for td in ltd:
            singleStudent.append(td.string)
        allStudents.append(singleStudent)

def printStudentsList():
    print(allStudents)
    print("{}  {}    {}".format("编号","姓名","分数"))
    for i in range(5):
        u=allStudents[i]
        print("{}   {}    {}".format(u[0],u[1],u[2]))
def main():
    url = 'https://www.cnblogs.com/exesoft/p/13184331.html'
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillStudentsList(soup)
    printStudentsList()
main()

 代码运行效果:

课堂练习-爬网(2)-爬网代码_第1张图片

你可能感兴趣的:(课堂练习-爬网(2)-爬网代码)