你們爬取網頁的時候,應該都遇到過這種狀況html
當我打印網頁源代碼的時候前端
發現 所有是亂碼的python
那這個時候應該怎麼辦呢?git
首先,response.content返回的內容 是二進制內容github
response.text 則是根據設置的encoding來解碼app
# 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的時候,ide
編碼是經過chardet.detect來獲取的,編碼
def apparent_encoding(self):
"""The apparent encoding, provided by the chardet library."""
return chardet.detect(self.content)['encoding']
複製代碼
那麼chardet.detect 又是幹嗎的呢?spa
簡單的講,就是根據給定的字節,來返回他的編碼code
至於他是如何實現的,歡迎去看源代碼。。。
上面說到了當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
複製代碼
原本呢,本篇文章到此結束了。。。可是呢。。。
有幾種方法能夠知道網頁的編碼呢?
方法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
若是還有猜想編碼的方法,歡迎留言
完...