python3爬虫编码问题

https://blog.csdn.net/lyxuefeng/article/details/79776751
使用爬虫爬取网页经常遇到各种编码问题,因此产生乱码

今天折腾了一天,全部总结一遍

环境:win10,pycharm,python3.4
1.首先先来网页编码是utf-8的:
以百度首页为例:
使用requests库
  1.  
    import requests
  2.  
     
  3.  
    url="http://www.baidu.com"
  4.  
    response = requests.get(url)
  5.  
    content = response.text
  6.  
    print(content)
结果有代码显示,但是出现乱码

使用urllib库
  1.  
    import urllib.request
  2.  
     
  3.  
    response = urllib.request.urlopen('http://www.baidu.com')
  4.  
    print(response.read())
结果有代码显示,但是以二进制返回

接下来介绍encode()和decode()方法
encode()用于解码,decode()方法用于编码
注:python3默认编码为utf-8
例1:
  1.  
    text = '中华'
  2.  
    print(type(text))
  3.  
    print(text.encode('gbk'))#以gbk形式解码,即把utf-8的字符串text转换成gbk编码
  4.  
    print(text.encode('utf-8'))#以utf-8形式解码,因为原本是utf-8编码,所以返回二进制
  5.  
    print(text.encode('iso-8859-1'))#报错
返回结果:
<class 'str'>
b'xd6xd0xbbxaa'
b'xe4xb8xadxe5x8dx8e'
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256)
为什么第四个报错?
我查寻了一下latin-1是什么?
Latin1是ISO-8859-1的别名,有些环境下写作Latin-1。
ISO-8859-1编码是单字节编码。
Unicode其实是Latin1的扩展。只有一个低字节的Uncode字符其实就是Latin1字符(这里认为unicode是两个字节,事实上因为各种版本不一样,字节数也不一样)
所以我的理解是:因为中文至少两个字节,所以不能解码出来

例2:
  1.  
    text = '中华'
  2.  
    print(type(text)) #<class 'str'>
  3.  
    text1 = text.encode('gbk')
  4.  
    print(type(text1)) #<class 'bytes'>
  5.  
    print(text1) #b'xd6xd0xbbxaa'
  6.  
    text2 = text1.decode('gbk')
  7.  
    print(type(text2)) #<class 'str'>
  8.  
    print(text2) #中华
  9.  
    text3 = text1.decode('utf-8') #报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
  10.  
    print(text3)
  11.  
     
  12.  
    text4= text.encode('utf-8')
  13.  
    print(type(text4)) #<class 'bytes'>
  14.  
    print(text4) #b'xe4xb8xadxe5x8dx8e'
  15.  
    text5 = text4.decode('utf-8')
  16.  
    print(type(text5)) #<class 'str'>
  17.  
    print(text5) #中华
  18.  
    text6 = text4.decode('gbk') #报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal multibyte sequence
  19.  
    print(text6)
为什么text3和text6会报错呢?
因为他们解码和编码使用的编码标准不一样。text1是用gbk解码,那么用utf-8编码回去就会报错,text6同理

好,回到百度例子,那么我们要怎么样才能看到我们想要的网页源代码呢?
使用requests库
  1.  
    import requests
  2.  
     
  3.  
    url="http://www.baidu.com"
  4.  
    response = requests.get(url)
  5.  
    content = response.text.encode('iso-8859-1').decode('utf-8')
  6.  
    #把网页源代码解码成Unicode编码,然后用utf-8编码
  7.  
    print(content)
使用urllib库
  1.  
    import urllib.request
  2.  
     
  3.  
    response = urllib.request.urlopen('http://www.baidu.com')
  4.  
    print(response.read().decode(utf-8))
2.关于网页源代码是gbk或者gb2312编码的网页:
  1.  
    import requests
  2.  
    response = requests.get('http://www.dytt8.net/')
  3.  
    #print(response.text)
  4.  
    html = response.text
  5.  
     
  6.  
    print(html)
结果返回乱码

  1.  
    import urllib.request
  2.  
    #get请求
  3.  
    response = urllib.request.urlopen('http://www.baidu.com')
  4.  
    print(response.read())
结果返回二进制

正确代码:
  1.  
    import requests
  2.  
    response = requests.get('http://www.dytt8.net/')
  3.  
    #print(response.text)
  4.  
    html = response.text.encode('iso-8859-1').decode('gbk')
  5.  
     
  6.  
    print(html)
  1.  
    import urllib.request
  2.  
    #get请求
  3.  
    response = urllib.request.urlopen('http://www.dytt8.net/')
  4.  
    print(response.read().decode('gbk'))

附:如何看网页源代码的编码格式?

使用F12查看网页源代码的head标签里的meta标签

如:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyxuefeng/article/details/79776751
原文地址:https://www.cnblogs.com/wangmo/p/9497173.html