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

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

當我列印網頁源代碼的時候

發現 全部是亂碼的

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

requests是如何判斷編碼

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

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

# Try charset from content-typencontent = Nonenencoding = self.encodingnnif not self.content:n return str()nn# Fallback to auto-detected encoding.nif self.encoding is None:n encoding = self.apparent_encodingnn# Decode unicode from given encoding.ntry:n content = str(self.content, encoding, errors=replace)nexcept (LookupError, TypeError):n

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

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

def apparent_encoding(self):n """The apparent encoding, provided by the chardet library."""n return chardet.detect(self.content)[encoding]n

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

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

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

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

那麼encoding 默認編碼是啥呢?繼續查看源代碼

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

response.encoding = get_encoding_from_headers(response.headers)n

def get_encoding_from_headers(headers):n """Returns encodings from given HTTP Header Dict.nn :param headers: dictionary to extract encoding from.n :rtype: strn """nn content_type = headers.get(content-type)nn if not content_type:n return Nonenn content_type, params = cgi.parse_header(content_type)nn if charset in params:n return params[charset].strip(""")nn if text in content_type:n return ISO-8859-1n

簡單講就是 如何返回頭裡面沒有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.txtwww.ietf.org

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

最後總結

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

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

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

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

再列印.text就可以了..

response.encoding = Nonenresponse.textn

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

科普個小知識

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

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

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

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

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

截圖自己看把,地址在

Move utility functions from requests.utils to requests-toolbelt · Issue #2266 · requests/requestsgithub.com圖標

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

完...

推薦閱讀:

即將發布的 tornado 2.0 將會帶來哪些特性?
第一天——畫一個圓
Fluent Python 筆記(二):序列基礎

TAG:Python | 爬虫计算机网络 | requests |