https://blog.csdn.net/lyxuefeng/article/details/79776751
使用爬蟲爬取網頁經常遇到各種編碼問題,因此產生亂碼
1.首先先來網頁編碼是utf-8的:
以百度首頁為例:
使用requests庫
使用urllib庫
接下來介紹encode()和decode()方法
encode()用於解碼,decode()方法用於編碼
注:python3默認編碼為utf-8
例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:
因為他們解碼和編碼使用的編碼標准不一樣。text1是用gbk解碼,那么用utf-8編碼回去就會報錯,text6同理
好,回到百度例子,那么我們要怎么樣才能看到我們想要的網頁源代碼呢?
使用requests庫
正確代碼:
今天折騰了一天,全部總結一遍
環境:win10,pycharm,python3.41.首先先來網頁編碼是utf-8的:
以百度首頁為例:
使用requests庫
-
import requests
-
-
url= "http://www.baidu.com"
-
response = requests.get(url)
-
content = response.text
-
print(content)
使用urllib庫
-
import urllib.request
-
-
response = urllib.request.urlopen( 'http://www.baidu.com')
-
print(response.read())
接下來介紹encode()和decode()方法
encode()用於解碼,decode()方法用於編碼
注:python3默認編碼為utf-8
例1:
-
text = '中華'
-
print(type(text))
-
print(text.encode( 'gbk'))#以gbk形式解碼,即把utf-8的字符串text轉換成gbk編碼
-
print(text.encode( 'utf-8'))#以utf-8形式解碼,因為原本是utf-8編碼,所以返回二進制
-
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:
-
text = '中華'
-
print(type(text)) #<class 'str'>
-
text1 = text.encode( 'gbk')
-
print(type(text1)) #<class 'bytes'>
-
print(text1) #b'\xd6\xd0\xbb\xaa'
-
text2 = text1.decode( 'gbk')
-
print(type(text2)) #<class 'str'>
-
print(text2) #中華
-
text3 = text1.decode( 'utf-8') #報錯:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
-
print(text3)
-
-
text4= text.encode( 'utf-8')
-
print(type(text4)) #<class 'bytes'>
-
print(text4) #b'\xe4\xb8\xad\xe5\x8d\x8e'
-
text5 = text4.decode( 'utf-8')
-
print(type(text5)) #<class 'str'>
-
print(text5) #中華
-
text6 = text4.decode( 'gbk') #報錯:UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 2: illegal multibyte sequence
-
print(text6)
因為他們解碼和編碼使用的編碼標准不一樣。text1是用gbk解碼,那么用utf-8編碼回去就會報錯,text6同理
好,回到百度例子,那么我們要怎么樣才能看到我們想要的網頁源代碼呢?
使用requests庫
-
import requests
-
-
url= "http://www.baidu.com"
-
response = requests.get(url)
-
content = response.text.encode( 'iso-8859-1').decode('utf-8')
-
#把網頁源代碼解碼成Unicode編碼,然后用utf-8編碼
-
print(content)
-
import urllib.request
-
-
response = urllib.request.urlopen( 'http://www.baidu.com')
-
print(response.read().decode(utf -8))
-
import requests
-
response = requests.get( 'http://www.dytt8.net/')
-
#print(response.text)
-
html = response.text
-
-
print(html)
-
import urllib.request
-
#get請求
-
response = urllib.request.urlopen( 'http://www.baidu.com')
-
print(response.read())
正確代碼:
-
import requests
-
response = requests.get( 'http://www.dytt8.net/')
-
#print(response.text)
-
html = response.text.encode( 'iso-8859-1').decode('gbk')
-
-
print(html)
-
import urllib.request
-
#get請求
-
response = urllib.request.urlopen( 'http://www.dytt8.net/')
-
print(response.read().decode( 'gbk'))
附:如何看網頁源代碼的編碼格式?
使用F12查看網頁源代碼的head標簽里的meta標簽
如:
