使用Python爬取最好大学网大学排名
作者:Lavi_qq_2910138025 时间:2023-09-17 09:24:45
本文实例为大家分享了Python爬取最好大学网大学排名的具体代码,供大家参考,具体内容如下
源代码:
#-*-coding:utf-8-*-
'''''
Created on 2017年3月17日
@author: lavi
'''
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def fillUnivList(univList,html):
soup = BeautifulSoup(html,"html.parser")
for tr in soup.find("tbody").children:
if isinstance(tr,bs4.element.Tag): #tobody有的节点是空串,属于要判断类型进行过滤
tds = tr("td") #等价于tr.find_all("td")
univList.append([tds[0].string,tds[1].string,tds[2].string]) #NavigableString可以跨越多个层次
def printUnivList(univList,num):
tplt = "{0:^6}\t{1:^10}\t{2:^6}" #:前的数字说明使用format函数的第几个参数填充模板
print(tplt.format("排名","学校名称","总分",chr(12288)))
for i in range(num):
u = univList[i]
print(tplt.format(u[0],u[1],u[2],chr(12288)))
def main():
url= "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html";
html = getHTMLText(url)
univList=[]
fillUnivList(univList,html)
printUnivList(univList,20)
main()
来源:http://blog.csdn.net/liuweiyuxiang/article/details/62897556
标签:Python,爬取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现读取txt文件中的数据并绘制出图形操作示例
2021-07-21 17:01:15
![](https://img.aspxhome.com/file/2023/6/66416_0s.png)
asp如何编写翻页函数?
2009-11-07 18:46:00
Python中识别图片/滑块验证码准确率极高的ddddocr库详解
2021-10-05 05:20:17
![](https://img.aspxhome.com/file/2023/2/97372_0s.png)
详解python中的闭包
2023-09-25 13:37:33
Python实现图像的垂直投影示例
2023-12-06 13:20:15
![](https://img.aspxhome.com/file/2023/1/76791_0s.jpg)
Python getattr()函数使用方法代码实例
2022-03-04 03:30:51
小试Python中的pack()使用方法
2021-02-03 06:00:43
![](https://img.aspxhome.com/file/2023/6/90706_0s.png)
在ASP中使用SQL语句之11:记录统计
2007-08-11 13:27:00
解决python3捕获cx_oracle抛出的异常错误问题
2023-01-21 17:51:26
python检索特定内容的文本文件实例
2022-12-29 12:05:42
Django模板标签{% for %}循环,获取制定条数据实例
2023-02-25 02:27:59
Python Pandas多种添加行列数据方法总结
2023-10-19 17:17:58
![](https://img.aspxhome.com/file/2023/2/110272_0s.png)
PHP数组交集的优化代码分析
2023-09-29 21:58:59
ASP保存远程图片到本地 同时取得第一张图片并创建缩略图的代码
2011-04-19 11:07:00
python双向循环链表实例详解
2023-08-04 04:53:06
JavaScript substr() 字符串截取函数使用详解
2024-06-05 09:55:13
python选择排序算法的实现代码
2021-06-19 07:51:10
python实现连连看游戏
2021-12-02 00:25:10
在Pycharm中使用GitHub的方法步骤
2021-04-27 04:46:48
![](https://img.aspxhome.com/file/2023/4/83654_0s.png)
python+opencv实现阈值分割
2023-05-19 11:23:50
![](https://img.aspxhome.com/file/2023/8/128448_0s.jpg)