AttributeError: ‘NoneType‘ object has no attribute ‘children‘ 错误(我同样遇到了,怎么解决,请看下文)

运行嵩天老师python爬虫课中第二周单元6中的实例1 “中国大学排名爬虫”会出现如下图错误:AttributeError: ‘NoneType’ object has no attribute ‘children’

我们正在学习大学慕课的这个课程,

Python网络爬虫与信息提取_北京理工大学_中国大学MOOC(慕课)

第一,url链接错误

 

 第二,string错误

 

2022年我们让做这个了。解决方法:url链接出了问题,应该是https://www.shanghairanking.cn/rankings/bcur/2022(因为我们是2022念做的,他原来的链接https://www.zuihaodaxue.com/zuihaodaxuepaiming2018.html无法实现爬虫)
这个网址就能输出最后结果了
还有,最后看评论听博主的,把string换成text,才行
网址和text缺一不可

好啦,问题解决了

最后附上我的程序,和运行结果

 

(这只是输出结果的一部分)

上代码:

import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(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")
    for tr in soup.find('tbody').children:
        if isinstance(tr,bs4.element.Tag):
            tds = tr ('td')
            ulist.append([tds[0].text,tds[1].text,tds[2].text])
def printUnivList (ulist,num):#要格式化输出
    print("{:^10}\t{:^6}\t{:^10}".format("排名","学校名称","总分"))
    for i in range (num):
        u=ulist[i]
        print("{:^10}\t{:^6}\t{:^10}".format(u[0],u[1],u[2]))
def main():     #主函数
    uinfo = []
    url = 'https://www.shanghairanking.cn/rankings/bcur/2022'
    html = getHTMLText(url)
    fillUnivList(uinfo,html)
    printUnivList (uinfo,20)#20所学校信息
main()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值