python(爬取大学排名)
2017-11-21 20:28
771 查看
import requests
from bs4 import BeautifulSoup
import bs4
def gethtml(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")
#arr=soup.find('tbody').contents #contents之后得到的是一个列表,
#children就是用来遍历的
#for tr in arr:
#for tr in soup.tbody.children:三种遍历方法都行
for tr in soup.find('tbody').children:
if isinstance(tr,bs4.element.Tag):
tds=tr('td') #把tr标签里面的td标签弄成列表的形式存起来
#print('tds=',tds[0]),等价于tds=tr.find_all('td')
ulist.append([tds[0].string,tds[1].string,tds[4].string])#取出tds列表中有用的三个,再次组合成一个列表,
#放进另一个列表中,相当于二维列表
def pr(ulist,num):
print("{:^10} {:^6} {:^10}".format("排名","学校","地址"))
for i in range(num):
u=ulist[i]
print("{:^10} {:^6} {:^10}".format(u[0],u[1],u[2]))
def main():
uinfo=[]
url='http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
html=gethtml(url)
fillUnivList(uinfo,html)
pr(uinfo,10)
main()
相关文章推荐
- Python爬虫实战:2017中国最好大学排名
- python3.x爬虫:爬取大学排名数据
- 使用Python爬取最好大学网大学排名
- python3----练习题(爬取电影天堂资源,大学排名,淘宝商品比价)
- 使用Python爬取最好大学网大学排名
- python_大学排名爬取
- Python3.6爬虫练习之爬取全国大学省份数据
- (PAT乙级)成绩排名(Python)
- Python爬虫实战(3):计算大学本学期绩点
- 2015-2016年全国计算机专业大学排名
- python get页面 并查找特定的数字来做成排名系统
- 美国大学最新排名较以往变化不大 名校对语言成绩(雅思)要求更高
- Python是美国主流大学最受欢迎的入门编程语言
- 2017年首份中美数据科学对比报告,Python受欢迎度排名第一,美国数据工作者年薪中位数高达11万美金
- 雅思专家告知大家如何正确看待美国大学最新排名
- python使用urllib模块和pyquery实现阿里巴巴排名查询
- python获取csdn的访问量和排名+falcon画图
- 2012-2013年世界大学计算机专业排名
- 日本の有名私立大学のランキング(排名)
- Python网络爬虫与信息提取-Day10-(实例)中国大学排名定向爬虫