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. x-www-form-urlencoded请求封装

    <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpc ...

  2. JavaScript 之 数组在内存中的存储方式(连续或不连续)

    最近在纠结一个问题,就是数组这个引用类型在JavaScript 中是不是和其他语言一样开辟了一个连续的内存来存储,但是在JS 中每个元素又可以是不同的类型,这就导致了没办法用一个相同大小的存储,所以数 ...

  3. MAC 关闭office自动更新提示

    ①.先进入到 /Library/Application Support/Microsoft/ 文件夹 (/Library/Application Support/Microsoft/) 点击 前往 - ...

  4. UI自动化之【chromedriver.exe无法删除问题】

    想删掉chromedriver.exe,结果提示被打开 在任务管理器中,找到Chromedriver.exe,结束进程

  5. Linux_ZABBIX实战

    typora-copy-images-to: img ZABBIX实战 zabbix安装 Zabbix详解 zabbix中文社区: http://www.zabbix.org.cn/ Zabbix中文 ...

  6. java-------导出数据csv

    outputStream = new FileOutoutStream(file); outputStreamWriter = new OutputStreamWriter (outputStream ...

  7. qtconsole和jupyter-notebook的对应启动命令行配置

    jupyter-notebook: set conda_path=D:\use\program\Miniconda3 pushd %conda_path% call Scripts\activate. ...

  8. Ubuntu-18.04.1-live-server-amd64.iso安装全过程

    以下向你介绍安装Ubuntu-18.04.1-live-server-amd64.iso版本的全过程,同时要说明的是Ubuntu 18.04.1 server版本只能通过文本的方式安装,即它没有和Ub ...

  9. hdu: You Are the One(区间DP)

    Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...

  10. python中将各种类型表达式中的 \ 符号,都看作转义符

    python中将各种类型表达式中的 \ 符号,都看作转义符,如 \n 代表换行  \t  代表空tab,还发现 \a 相当于什么都不是,改行末尾的 \ 的意思是该行还没写完,接着下边一行的内容. 转义 ...