嵩天老师课程,爬取大学排名的练习扩展

对齐搞了好久(数据对齐,目录会调),不搞了,有大佬的话可以指点一下谢谢了

import requests
from bs4 import BeautifulSoup
import bs4

def getHTMLText(url):
     try:
          kv = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)\
               Chrome/80.0.3987.132 Safari/537.36'}
          r = requests.get(url,timeout = 30,headers = kv)
          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")#等价于tds = tr.find_all("td")
               ulist.append([tds[0].string,tds[1].string,tds[2].string,tds[3].string,\
                             tds[4].string,tds[5].string,tds[6].string,tds[7].string,\
                             tds[8].string,tds[9].string,tds[10].string,tds[11].string])

def printUnivList(ulist,num):
     print("{0:^10}\t{1:{12}^20}\t{2:{12}<15}\t{3:{12}<10}\t{4:{12}<10}\t{5:{12}^10}\t{6:{12}<10}\t{7:{12}<10}\t\
          {8:{12}<10}\t{9:{12}<10}\t{10:{12}<10}\t{11:{12}<10}".format\
                ("排名","学校名称","地址","高考成绩得分","毕业生就业率","论文数量","论文质量","被引用论文","被引用学者"\
                 ,"企业科研经费(千元)","校企合作论文","技术转让收入(千元)",chr(12288)))
     for i in range(num):
          u = ulist[i]
          print("{0:^10}\t{1:{12}^20}\t{2:{12}<15}\t{3:{12}<10}\t{4:{12}<10}\t{5:{12}<10}\t\t{6:{12}<10}\t{7:{12}<10}\t\
          {8:{12}<10}\t{9:{12}<10}\t{10:{12}<10}\t{11:{12}<10}".format\
          (u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7],u[8],u[9],u[10],u[11],chr(12288)))
     print("Suc"+str(num))

def main():
     uinfo = []
     url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html"
     html = getHTMLText(url)
     fillUnivlist(uinfo,html)
     printUnivList(uinfo,20)

main()

运行结果:
嵩天老师课程,爬取大学排名的练习扩展_第1张图片
嵩天老师课程,爬取大学排名的练习扩展_第2张图片

你可能感兴趣的:(嵩天老师课程,爬取大学排名的练习扩展)