爬取中国大学排名网站内容
Posted tqianying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取中国大学排名网站内容相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 16:05:31 2020
"""
Created on Wed May 13 16:05:31 2020
@author: CHANYING
"""
"""
import requests
from bs4 import BeautifulSoup
allUniv=[]
def get(url):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return ""
def f(soup):
data=soup.find_all(‘tr‘)
fw=open("pq2015.csv","w",encoding=‘utf-8‘)
for tr in data:
ltd=tr.find_all(‘td‘)
if len(ltd)==0:
continue
s=[]
for td in ltd:
s.append(td.string)
allUniv.append(s)
fw.write(",".join(s)+" ")
fw.close()
def p(num):
print("{1:^4}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10}{6:{0}^10}{7:{0}^10}".format(chr(12288),"排名","学校名称","省市","总分","人才培养得分","科学研究得分","社会服务得分"))
for i in range(num):
u=allUniv[i]
print("{1:^4}{2:{0}^11}{3:{0}^6}{4:{0}^7.1f}{5:{0}^10}{6:{0}^12}{6:{0}^10}".format(chr(12288),u[0],u[1],u[2],eval(u[3]),eval(u[4]),eval(u[5]),eval(u[6])))
def main(num):
u=‘http://www.zuihaodaxue.cn/zuihaodaxuepaiming2015_0.html‘
h=get(u)
soup=BeautifulSoup(h,"html.parser")
f(soup)
p(num)
main(30)
from bs4 import BeautifulSoup
allUniv=[]
def get(url):
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return ""
def f(soup):
data=soup.find_all(‘tr‘)
fw=open("pq2015.csv","w",encoding=‘utf-8‘)
for tr in data:
ltd=tr.find_all(‘td‘)
if len(ltd)==0:
continue
s=[]
for td in ltd:
s.append(td.string)
allUniv.append(s)
fw.write(",".join(s)+" ")
fw.close()
def p(num):
print("{1:^4}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10}{6:{0}^10}{7:{0}^10}".format(chr(12288),"排名","学校名称","省市","总分","人才培养得分","科学研究得分","社会服务得分"))
for i in range(num):
u=allUniv[i]
print("{1:^4}{2:{0}^11}{3:{0}^6}{4:{0}^7.1f}{5:{0}^10}{6:{0}^12}{6:{0}^10}".format(chr(12288),u[0],u[1],u[2],eval(u[3]),eval(u[4]),eval(u[5]),eval(u[6])))
def main(num):
u=‘http://www.zuihaodaxue.cn/zuihaodaxuepaiming2015_0.html‘
h=get(u)
soup=BeautifulSoup(h,"html.parser")
f(soup)
p(num)
main(30)
以上是关于爬取中国大学排名网站内容的主要内容,如果未能解决你的问题,请参考以下文章
python-爬取中国大学排名网站信息IndexError:list index out of range