再也不用擔心網頁編碼的坑了!

kimg1234發表於2019-02-16

大家爬取網頁的時候,應該都遇到過這種情況

當我列印網頁原始碼的時候

發現 全部是亂碼的

再也不用擔心網頁編碼的坑了!

那這個時候應該怎麼辦呢?

requests是如何判斷編碼

首先,response.content返回的內容 是二進位制內容

response.text 則是根據設定的encoding來解碼

# Try charset from content-type  
content = None  
encoding = self.encoding  
  
if not self.content:  
  return str('')  
  
# Fallback to auto-detected encoding.  
if self.encoding is None:  
    encoding = self.apparent_encoding  
  
# Decode unicode from given encoding.  
try:  
    content = str(self.content, encoding, errors='replace')  
except (LookupError, TypeError):

複製程式碼

我們可以看到 ,當encoding為None的時候,

編碼是通過chardet.detect來獲取的,

def apparent_encoding(self):  
"""The apparent encoding, provided by the chardet library."""  
    return chardet.detect(self.content)['encoding']
複製程式碼

那麼chardet.detect 又是幹嘛的呢?

簡單的講,就是根據給定的位元組,來返回他的編碼

至於他是如何實現的,歡迎去看原始碼。。。

上面說到了當encoding為None的時候,requests是如何設定encoding的

那麼encoding 預設編碼是啥呢?繼續檢視原始碼

我們在adapters.py 裡面找到了~

response.encoding = get_encoding_from_headers(response.headers)

def get_encoding_from_headers(headers):  
"""Returns encodings from given HTTP Header Dict.  
    :param headers: dictionary to extract encoding from.  
    :rtype: str  
    """  
    content_type = headers.get('content-type')  
  
if not content_type:  
  return None  
content_type, params = cgi.parse_header(content_type)  

if 'charset' in params:  
  return params['charset'].strip("'\"")  
  
if 'text' in content_type:  
  return 'ISO-8859-1'
複製程式碼

簡單講就是 如何返回頭裡面沒有content_type,則encoding為None

如果charset在引數裡面的話,則使用charset設定的值(看下圖,github返回的)

再也不用擔心網頁編碼的坑了!

如果text在引數裡面的話,則使用ISO-8859-1

然後你列印下 你亂碼網頁的encoding,發現,還真是ISO-8859-1

再也不用擔心網頁編碼的坑了!

你會很奇怪,為啥當content-type為text/html的時候,編碼為iso-8859-1呢?

再也不用擔心網頁編碼的坑了!

現在常見的編碼不是utf8麼,requests怎麼這麼傻*呢...

然後發現是rfc2016的規定。。。

rfc2016的連結在 https://www.ietf.org/rfc/rfc2616.txt

感興趣的同學可以自行查閱...

再也不用擔心網頁編碼的坑了!

最後總結

當返回頭沒有content_type 的時候,encoding使用chardet.detect 猜測出來的編碼(一般都是很準的)

當返回頭裡面有content_type 的時候,如果有charset=xxx,則encoding的編碼為chatset的值。如果只是text/html,則編碼為ISO-8859-1

那麼當你發現response.text返回亂碼的時候,怎麼辦呢。。。

只要先設定編碼為None...

再列印.text就可以了..

response.encoding = None  
response.text
複製程式碼

本來呢,本篇文章到此結束了。。。但是呢。。。

科普個小知識

有幾種方法可以知道網頁的編碼呢?

  1. 我們上面講過的 response.headers中的content_type
  2. 通過chardet.detect猜測出來(上面講過的)
  3. 網頁原始碼中的 meta(且有charset的值)如下面的,則表示網頁編碼為gb2312(不過呢,有時候並不是很準,這個是前端瞎xx寫的,這時候就可以用chardet.detect來猜測了...)

方法3的程式碼如何寫呢(如下)

def get_encodings_from_content(content):  
"""Returns encodings from given content string.  
    :param content: bytestring to extract encodings from.  
    """  
    warnings.warn((  
'In requests 3.0, get_encodings_from_content will be removed. For '  
        'more information, please see the discussion on issue #2266. (This'  
        ' warning should only appear once.)'),  
        DeprecationWarning)  
  
    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)  
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)  
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')  
  
    return (charset_re.findall(content) +  
            pragma_re.findall(content) +  
            xml_re.findall(content))
複製程式碼

你會看到requests3.0版本的時候,這個方法會去掉,這又是為什麼呢。。。

截圖自己看把,地址在https://github.com/requests/requests/issues/2266

再也不用擔心網頁編碼的坑了!

如果還有猜測編碼的方法,歡迎留言

完...

相關文章