嵩天老师python网课爬虫实例1的问题和解决方法

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

很明显,报错的意思是说tbody下面没有children,说明我们在gethtmltext的时候可能出现了问题,可以用print(r.status.code)测试,发现并不是200,print(r.raise_for_status())返回的值也是None ,其次 gethtmltext返回的也是 error,说明我们并没有成功下载网页源码。错误原因猜测

1,zuihaodaxue.com网站采取了反爬机制

2,由于教程录制时间久远,url网址错误

第一种情况,加上代理头和cookies测试,发现一样提示 AttributeError: 'NoneType' object has no attribute 'children' ,最后发现是我自己 r.text 写成了 r.txt

def gethtmltext(url):
    headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"}
    cooks={"cookies":"Hm_lvt_2ce94714199fe618dcebb5872c6def14=1558142987; Hm_lpvt_2ce94714199fe618dcebb5872c6def14=1558147316"}
    try:
        r=requests.get(url,headers=headers,cookies=cooks,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "error"

2,发现老师给的网址是zuihaodaxue.cn,现在网站更新变成了.com,所以换成 http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html 即可解决问题

如果还存在问题那么大部分原因可能是单词拼写错误。

以下为正确代码:

 1 def gethtmltext(url):
 2     try:
 3         r=requests.get(url,timeout=30)
 4         r.raise_for_status()
 5         r.encoding=r.apparent_encoding
 6         return r.text
 7     except:
 8         return "error"
 9 
10 if __name__ == '__main__':
11     url="http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html"

二,format()函数格式错误——ValueError: Invalid format specifier

以下代码错误:提示ValueError: Invalid format specifier

1 def printunivlist(urlist,num):
2     tplt = "{0:^6}\t{1:{3}^10}\t{2:^10}\t{3:^10}"
3     print(tplt.format("排名","学校","地区","总分",chr(12288)))
4     for i in range(num):
5         u = urlist[i]
6         print(tplt.format(u[0],u[1],u[2],u[3],chr(12288)))

以下代码正确:

1 def printunivlist(urlist,num):
2     tplt = "{0:^6}\t{1:{4}^10}\t{2:^10}\t{3:^10}"
3     print(tplt.format("排名","学校","地区","总分",chr(12288)))
4     for i in range(num):
5         u = urlist[i]
6         print(tplt.format(u[0],u[1],u[2],u[3],chr(12288)))

可以看出,仅仅是1:{3}^10和1:{4}^10的差别。

原因分析:

第二行改为#这里添加了tplit = "{0:^10}\t{1:{3}^10}\t{2:^10}" ;{3:^10}”你添加了地区,相应的作为填充不足10个字符长度的chr(12288)已经不是3了,而是4。

在这里很多同学肯定会问{1:{3}^10},填充为什么是填充3个或4个,为什么是在1号位填充:

第一,中英文全半角造成不对齐的原因产生在1号位;

第二,分析实例【Python爬取中国前20强大学】前20大学的结果,为排名、学校名称、总分,3个地方需填充,即为3;

第三:后面加入省市,为排名、学校名称、总分、省市,4个地方需要填充,即为4;

转载来源:https://blog.csdn.net/Andone_hsx/article/details/84025828

最后贴上我的代码:

 1 import requests
 2 from bs4 import BeautifulSoup
 3 import bs4
 4 
 5 def gethtmltext(url):
 6     try:
 7         r=requests.get(url,timeout=30)
 8         r.raise_for_status()
 9         r.encoding=r.apparent_encoding
10         return r.text
11     except:
12         return "error"
13 
14 def fillunivlist(urlist,html):
15     soup = BeautifulSoup(html,"html.parser")
16     for tr in soup.find("tbody").children:
17         if isinstance(tr,bs4.element.Tag):
18             tds = tr("td") #将所有标签存为一个列表
19             urlist.append([tds[0].string,tds[1].string,tds[2].string,tds[4].string])
20 
21 
22 
23 def printunivlist(urlist,num):
24     tplt = "{0:^6}\t{1:{4}^10}\t{2:^10}\t{3:^10}"
25     print(tplt.format("排名","学校","地区","总分",chr(12288)))
26     for i in range(num):
27         u = urlist[i]
28         print(tplt.format(u[0],u[1],u[2],u[3],chr(12288)))
29 
30 if __name__ == '__main__':
31     unifo=[]
32     url="http://www.zuihaodaxue.com/zuihaodaxuepaiming2019.html"
33     html=gethtmltext(url)
34     fillunivlist(unifo,html)
35     printunivlist(unifo,20)

 

猜你喜欢

转载自www.cnblogs.com/tqing/p/10884951.html