• 网络爬虫简单入门--数据抓取-数据解析-数据显示-数据入库-B


    代码1:

    a=[3.45,4.45,5]
    b=[5,4]
    c=["aa",456,True]
    myList=[]
    myList.append(a)
    myList.append(b)
    myList.append(c)
    print(myList)

    代码2:

    #coding=utf-8
    list=[]
    for i in range(1,101):
        list.append(i)
    
    # print(list)
    
    tempList=[]
    newList=[]
    
    while True:
        num=0
        for temp in list:
            tempList.append(temp)
            num+=1
            if num==3:
                newList.append(tempList)
                tempList=[]
                num=0
                continue
        if temp==100:
            newList.append(tempList)
            break
    
    print(newList)

    代码3:

    import requests
    from bs4 import BeautifulSoup
    allUniv = []
    def getHTMLText(url):
        try:
            r = requests.get(url, timeout=30)
            r.raise_for_status()
            r.encoding = 'utf-8'
            return r.text
        except:
            return ""
    def fillUnivList(soup):
        data = soup.find_all('tr')
        for tr in data:
            ltd = tr.find_all('td')
            if len(ltd)==0:
                continue
            singleUniv = []
            for td in ltd:
                singleUniv.append(td.string)
            allUniv.append(singleUniv)
    def printUnivList(num):
        print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))
        for i in range(num):
            u=allUniv[i]
            print("{:^4}{:^10}{:^5}{:^8}{:^10}".format(u[0],u[1],u[2],u[3],u[6]))
    def main():
        url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
        html = getHTMLText(url)
        soup = BeautifulSoup(html, "html.parser")
        fillUnivList(soup)
        printUnivList(10)
    main()

    课后作业:

    1.复制上述代码,在Python环境下运行。

    2.读懂上述代码。

  • 相关阅读:
    ueditor单独调用图片上传
    百度Ueditor多图片上传控件
    linux基础之vim编辑器
    linux基础之进阶命令二
    linux基础之基础命令一
    Python基础之PyCharm快捷键大全
    IT菜鸟之VTP应用项目
    IT菜鸟之总结(Du teacher)
    IT菜鸟之DHCP
    IT菜鸟之路由器基础配置(静态、动态、默认路由)
  • 原文地址:https://www.cnblogs.com/exesoft/p/12988105.html
Copyright © 2020-2023  润新知