import requests
from bs4 import BeautifulSoup
import bs4 def getHTMLText(url):
try:
r = requests.get(url,timeout=30)
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')
ulist.append([tds[0].text.strip(),tds[1].text.strip(),tds[4].text.strip()]) # strip函数移除空格或换行符 def printUnivList(ulist,num):
tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}" # {3}表示使用format的第三个变量,即chr(12288)中文字符填充
print("{:^9}\t{:^15}\t{:^8}".format("排名","学校名称","分数"))
for i in range(num):
u = ulist[i]
print(tplt.format(u[0],u[1],u[2],chr(12288))) def main():
uinfo = []
url = "https://www.shanghairanking.cn/rankings/bcur/2020"
html = getHTMLText(url)
fillUnivList(uinfo,html)
printUnivList(uinfo,20) # 前20所 main()

2020中国最好大学排名.py(亲测有效)的更多相关文章

  1. Python爬虫实战:2017中国最好大学排名

    抓取内容: 从最好大学网上抓取中国前10的大学排名.大学名称.总分,并数据格式左对齐. http://www.zuihaodaxue.cn/zuihaodaxuepaiming2017.html 首先 ...

  2. python爬虫抓取中国最好大学排名1 清华大学 北京 94.0 100.0 97.70% 清华大学 2 北京大学

    import requests from bs4 import BeautifulSoup import bs4.element r=requests.get("http://www.zui ...

  3. python爬虫学习(二):定向爬虫例子-->使用BeautifulSoup爬取"软科中国最好大学排名-生源质量排名2018",并把结果写进txt文件

    在正式爬取之前,先做一个试验,看一下爬取的数据对象的类型是如何转换为列表的: 写一个html文档: x.html<html><head><title>This is ...

  4. Intellij IDEA 2020.1.1 破解 永久有效 亲测100%成功

    申明:本教程 WebStorm 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除. 前言 作为一个有强迫症的码农,怎么能忍受自己的开发工具跟不上潮流呢?笔者以前一直用 ...

  5. python爬虫入门---第二篇:获取2019年中国大学排名

    我们需要爬取的网站:最好大学网 我们需要爬取的内容即为该网页中的表格部分: 该部分的html关键代码为: 其中整个表的标签为<tbody>标签,每行的标签为<tr>标签,每行中 ...

  6. Python之爬虫-中国大学排名

    Python之爬虫-中国大学排名 #!/usr/bin/env python # coding: utf-8 import bs4 import requests from bs4 import Be ...

  7. python网络爬虫-中国大学排名定向爬虫

    爬虫定向爬取中国大学排名信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup import bs4 #从网络上获取大学 ...

  8. python爬虫学习心得:中国大学排名(附代码)

    今天下午花时间学习了python爬虫的中国大学排名实例,颇有心得,于是在博客园与各位分享 首先直接搬代码: import requests from bs4 import BeautifulSoup ...

  9. python爬取中国大学排名

    教程来自:[Python网络爬虫与信息提取].MOOC. 北京理工大学 目标:爬取最好大学网前50名大学 代码如下: import requests from bs4 import Beautiful ...

  10. Pycharm 2020.01亲测激活到2089年

    Pycharm 2020.01亲测激活到2089年,注意是2020.01版本. 下载jetbrains-agent.jar 链接: https://pan.baidu.com/s/1w1i15pT38 ...

随机推荐

  1. 解决:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH. See README file for more information.

    问题:使用pytesseract库识别图片中文字时出现报错 代码: import pytesseract from PIL import Image,ImageEnhance img=Image.op ...

  2. backward函数中gradient参数的一些理解

    当标量对向量求导时不需要该参数,但当向量对向量求导时,若不加上该参数则会报错,显示"grad can be implicitly created only for scalar output ...

  3. Flutter基于Mac的环境配置

    1.下载Flutter SDK git clone https://github.com/flutter/flutter.git 2.配置Flutter镜像+Flutter环境变量 ( 先创建.bas ...

  4. sequelize 分页查询

    分页原理: 第一步 下面的result 可以修改:

  5. 05 js利用ajax向服务器发送请求并返回json值

    创建一个php $json = '';$data = array();$con =mysqli_connect($servername, $username, $password, $dbname); ...

  6. MyBatis Generator使用方法

    第一步:在resources文件夹下创建一个目录mybatis-generator,在目录mybatis-generator下创建文件generatorConfig.xml(此处的目录名可任意取) 第 ...

  7. 【Android报错】FileNotFoundException open failed:文件路径 EPERM (Operation not permitted)外部存储至根目录报错,Android外部存储权限动态获取问题

    报错:FileNotFoundException open failed: XXXXXXX EPERM (Operation not permitted) 查了下,大概原因是因为权限的问题.(小白学A ...

  8. ES6知识点总结

    声明变量      let 不能重复声明 块级作用域 可修改let变量的值 , const 不能重复声明 块级作用域 不能修改const 变量的值 2. 箭头函数 而箭头函数的this指向函数定义时所 ...

  9. vue、react配置gzip打包后,删除源文件deleteOriginalAssets: true,nginx需要的配置

    1.删除源文件后,配置了gzip,当配置gzip删除源文件后,解决前端history问题,就会出现所有的都返回html,请求js.css也会返回html,页面会报错,如下配置即可 location / ...

  10. 面向对象ooDay8

    精华笔记: 接口: 是一种数据类型(引用类型) 由interface定义 只能包含常量和抽象方法(所有数据默认都是常量,所有方法默认都是抽象的) 接口不能被实例化 接口是需要被实现/继承的,实现/派生 ...