python爬虫入门常见错误集合

在入门爬虫的时候遇到不少问题,和不是唯一的解决方法的方法,总结整理一下,供大家学习交流。

  1. syntaxerror: invalid syntax语法错误:无效语法

  2. syntaxerror: unexpected EOF while parsing语法错误:多了无法解析的符号(检查是否多了或少了括号)

  3. syntaxerror: invalid character in identifier语法错误:有无效标识符(检查中文符号)

  4. indexerror: list index out of range索引错误:列表超出索引范围(检查列表是否为空)

  5. typeerror: must be str, not int类型错误:数据不是正确的数据类型,比如字符串和数字直接拼接(检查数据类型)

  6. indentationerror: expected an indented block缩进错误:检查代码的缩进是否正确

  7. valueerror: substring not found值错误:输入的数据类型跟要求不符合

  8. nameerror: name ‘a’ is not defined未初始化对象,变量没有被定义

  9. attributeerror: ‘tuple’ object has no attribute 'remove’属性错误:该对象没有这个属性、方法(检查数据类型)

10.网页出现乱码,出现乱码的原因是因为网页解码过程中没有设置如何编码
1)response.encoding = response.apparent_encoding

11.请求头参数

InvalidHeader: Invalid return character or leading space in header: User-Agent

import requests

headers = {
    
    
    'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4128.3 Safari/537.36'
}
response = requests.get('http://www.shuquge.com/txt/8659/index.html', headers=headers)
response.encoding = response.apparent_encoding
html = response.text
print(html)

其实很难发现问题在哪,但事实上是因为‘ Mozilla’之前多了个空格,把空格删去即可
12. requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘134.175.188.27’, port=5010): Max retries exceeded with url: /get (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x0000023AB83AC828>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。’,))
目标计算机积极拒绝

被识别了
网址输入错误了
服务器停止提供服务器了

总的来说,一定要耐心,没有解决不了的问题,兴趣是最好的方法。

猜你喜欢

转载自blog.csdn.net/qq_41600018/article/details/109111380
今日推荐