爬取软科-中国大学排行榜

爬取软科中国大学排行榜

在这里插入图片描述
要求最后得到的文本爬取的格式如下
在这里插入图片描述

import requests

r=requests.get('http://www.shanghairanking.cn/rankings/bcur/2020')

r.status_code

r.encoding=r.apparent_encoding

r.text
##输出展示文本
school=r.text

from bs4 import BeautifulSoup

soup=BeautifulSoup(school,'html.parser')

soup.tbody

soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[0].string.replace('\n','').replace(' ','')
##输出“1”
soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[1].a.string
##'清华大学'
soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[2].string.replace('\n','').replace(' ','')
##‘北京’
soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[3].string.replace('\n','').replace(' ','')
##‘综合’
soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[4].string.replace('\n','').replace(' ','')
##‘852.5’
soup.find_all('tbody')[0].find_all('tr')[0].find_all('td')[5].string.replace('\n','').replace(' ','')
##'38.2'

整理,利用for循环输出排名

for t in soup.find_all('tbody')[0].find_all('tr'):
    print(t.find_all('td')[0].string.replace('\n','').replace(' ',''),
         t.find_all('td')[1].a.string,
         t.find_all('td')[2].string.replace('\n','').replace(' ',''),
         t.find_all('td')[3].string.replace('\n','').replace(' ',''),
         t.find_all('td')[4].string.replace('\n','').replace(' ',''),
         t.find_all('td')[5].string.replace('\n','').replace(' ',''))

在这里插入图片描述
完成输出,具体导出呈txt或相关文件,可以看一下美团或者相亲的那个导出方法。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值