• python简单爬虫(中国大学排名)


     1 import requests
     2 from bs4 import BeautifulSoup
     3 import bs4
     4 
     5 def getHTMLText(url):
     6     try:
     7         r = requests.get(url, timeout=30)
     8         r.raise_for_status()
     9         r.encoding = r.apparent_encoding
    10         return r.text
    11     except:
    12         return ""
    13 
    14 def fillUnivList(ulist, html):
    15     soup = BeautifulSoup(html, "html.parser")
    16     for tr in soup.find('tbody').children:
    17         if isinstance(tr, bs4.element.Tag):
    18             tds = tr('td')
    19             ulist.append([tds[0].string, tds[1].string, tds[3].string])
    20 
    21 def printUnivList(ulist, num):
    22     tplt = "{0:^10}	{1:{3}^10}	{2:^10}"
    23     print(tplt.format("排名","学校名称","总分",chr(12288)))
    24     for i in range(num):
    25         u=ulist[i]
    26         print(tplt.format(u[0],u[1],u[2],chr(12288)))
    27     
    28 def main():
    29     uinfo = []
    30     url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    31     html = getHTMLText(url)
    32     fillUnivList(uinfo, html)
    33     printUnivList(uinfo, 20) # 20 univs
    34 main()
  • 相关阅读:
    DBA 职责及日常工作职责
    mysql 语句笔记
    学习Groovy — 闭包
    linux 命令: 网络和进程
    linux命令: rm命令
    Pro Git-- 跟踪分支
    Redis Tutorial
    linux 命令--文件操作
    分库分表的几种常见形式
    乐观锁与悲观锁
  • 原文地址:https://www.cnblogs.com/ouzai/p/13035381.html
Copyright © 2020-2023  润新知