错误1:list index out of range
原因:在用lxml和xpath对一个网站进行解析,在解析的时候出现错误-IndexError: list index out of range
是在中这个网站的html代码中有的标识为空,只要加上
try:
.....
except:
pass
错误机制跳过空值就行了
错误2:'utf-8' codec can't decode byte 0xd3
因为 原因是因为这里的0xd3,这是无法转换出来的部分,这是不属于编码字符集中的部分。所以,在进行编码转换的时候,会报错。
解决 修改字符集参数,一般这种情况出现得较多是在国标码(GBK)和utf8之间选择出现了问题。
出现异常报错是由于设置了decode()方法的第二个参数errors为严格(strict)形式造成的,因为默认就是这个参数,将其更改为ignore
错误3:'utf-8' codec can't decode byte 0x8b
因为相应的数据被压缩了 变成zip的格式 需要解压
所以这个函数来解压 text=ungzip(rest.read()).decode('utf-8')
import gzip
def ungzip(data):
try:
data=gzip.decompress(data)
except:
pass
return data
错误4:TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
post 请求必须是byte类型 encode('utf-8') 将str 转为 byte
错误5:TypeError: write() argument must be str, not bytes
这里 decode('utf-8') 是 因为文件写入必须为 str decode 将byte 转为str
错误6:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 251: invalid start byte
decode('utf-8')不行 需要 decode('jbk')
因为 <META http-equiv=Content-Type content="text/html; charset=gb2312"> charset=gb2312 这为jbk
对于这个 <META http-equiv=Content-Type content="text/html; charset=gb2312"> 一般在网页源代码 最前面看一看
我也不知道这是不是好的解决方法 我自己做过得,应该可以得行吧
如果有错误的 也欢迎各位指出
要是有好的方法 欢迎留言 一起学习交流交流