AttributeError: 'NoneType' object has no attribute 'children', 网页'tbody'没有子类

源码

import requests
import bs4
from bs4 import BeautifulSoup
def getHTMLText(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        print(chucuo)

def fillUnivList(ulist,html):
    soup = BeautifulSoup(html,"html.parser")
    for tr in soup.find('tbody').children:
        if isinstance(tr,bs4.element.Tag):        #需bs4库,如果参数不是一个给定类型的对象,返回假
            tds = tr('td')
            ulist.append([tds[0].sting,tds[1].sting,tds[3].sting])

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 = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html"
    html = getHTMLText(url)
    fillUnivList(uinfo,html)
    printUnivList(uinfo,20)

main()

可能原因分析
soup.find(‘tbody’).children: ‘tbody’内容出错,尝试输出 tbody

def fillUnivList(ulist,html):
    soup = BeautifulSoup(html,"html.parser")
    print(1)
    print(soup.find('tbody'))
    print(3)

输出为
1
None
3
即未搜索到 tbody 结果

确认 r.text 内容

def getHTMLText(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print(1)
        print(r.text)
        print(3)
        return r.text

输出
1
Your browse does not support frame!
3
即 r.text出错
有信息输出,表示网页已成功连接上,

>>> import requests
>>> r = requests.get("http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html")
>>> r.status_code
200
>>> r.encoding
'ISO-8859-1'
>>> r.encoding = r.apparent_encoding
>>> r.encoding
'ascii'
>>> r.text
' </span> <span class="hljs-xmlDocTag"><body></span>Your browse does not support frame!<span class="hljs-xmlDocTag"></body></span> <span class="hljs-xmlDocTag">  '
>>> 

1、网址更改为百度 返回正常;2、网址在浏览器打开正常;3、网站没有robot协议,不存在爬虫受限情况;4、浏览器打开”http://www.zuihaodaxue.cn/“, 发现被管理员禁止了,公司禁网了,就是不知道为什么它的下阶网站不禁;

你可能感兴趣的:(AttributeError: 'NoneType' object has no attribute 'children', 网页'tbody'没有子类)