Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python网络爬虫

使用Python爬取最好大学网大学排名

来源:中文源码网    浏览:172 次    日期:2024-04-29 04:53:14
【下载文档:  使用Python爬取最好大学网大学排名.txt 】


使用Python爬取最好大学网大学排名
本文实例为大家分享了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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。

相关内容