Python爬去最好大学排名代码

Python爬取最好大学排名代码:import bs4from urllib import requestfrom bs4 import BeautifulSoup’’’()获取网站页面’’‘def getHTMLText(url): try: resp=request.urlopen(url) html_data=resp.read().decode(‘utf-8’) return html_data except: return “”’’’(2)处理页面,提取相关信息’’‘def fillUnivList(ulist,html): soup=BeautifulSoup(html,“html.parser”) for tr in soup.find(‘tbody’).children: #搜索’tbody’后面的子节点 if isinstance(tr,bs4.element.Tag): tds=tr(‘td’) ulist.append([tds[0].text.strip(),tds[1].text.strip(),tds[3].text.strip()])’’’(3)解析数据,格式化输出结果’’'def printUnivList(ulist,num): tplt="{0:10}\t{1:{3}10}\t{2:^10}" print(tplt.format(“排名”,“学校名称”,“总分”,chr(12288))) for i in range(num): u=ulist[i] print(tplt.format(u[0],u[1],u[2],chr(12288)))if name==‘main’: uinfo=[] url=‘https://www.shanghairanking.cn/rankings/bcur/2020’ html=getHTMLText(url) fillUnivList(uinfo,html) printUnivList(uinfo,10)在这里插入图片描述
代码运行过程中TypeError: unsupported format string passed to NoneType.__format代码:ulist.append([tds[0].string,tds[1].string,tds[2].string])改为:ulist.append([tds[0].text.strip(),tds[1].text.strip(),tds[2].text.strip()])(网页格式已经改变了,要删除的多余空格,用.strip());AttributeError: ‘NoneType’ object has no attribute ‘children’ 错误意思是 ‘NoneType’ 对象没有属性 ‘children’ ,这个错误说明’children’ 属性的对象 soup 是一个空类型,那就意味着soup = BeautifulSoup(html,‘html.parser’)中soup并没有得到解析出来的html页面,那就是说在调用getHTMLText(url)函数时这个函数并没有得到url链接对应的网页信息。错误就出在getHTMLText(url)函数之中,可是仔细审查一遍后发现并没有错误。那所有的所有都指向了最后的一个可能,真相只有一个,那就是url地址有问题。更新网址。格式化字符串 IndexError: Replacement index 5 out of range for positional args tuple可能原因:1、格式化方法提供了0~5的6个占位符,但是format()内只有5个变量。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值