Reputation: 115
Parser to get a list of best universities in China. I get 'NoneType' object has no attribute 'children'' error message:
File "C:/Projects/Beijing Python/Week 2/Unit 06.py", line 22, in fillUnivList for tr in soup.find('tbody').children:
AttributeError: 'NoneType' object has no attribute 'children'
If I check mannually, "soup.find('tbody').children" statement is ok. What's wrong?
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_stasus()
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].string, tds[1].string, tds[3].string])
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()
Upvotes: 2
Views: 1128
Reputation: 8392
I'm assuming that your request
object doesn't have raise_for_stasus()
. It is a typo - it's raise_for_status()
. Also you can do with status_code
.
Doing
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
if r.status_code == 200:
r.encoding = r.apparent_encoding
return r.text
except Exception, e:
print (e)
return ""
And changing your find
to for tr in soup.find('tbody', class_="hidden_zhpm").children:
Seems to work.
Upvotes: 1