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'\xd6\xd0\xbb\xaa'
b'\xe4\xb8\xad\xe5\x8d\x8e'
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'\xd6\xd0\xbb\xaa'
  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'\xe4\xb8\xad\xe5\x8d\x8e'
  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM