用以下代碼獲取了網(wǎng)頁(yè)內(nèi)容之后,解碼再編碼print出來(lái)的中文部分是亂碼。可能是什么原因
import chardet # 一個(gè)檢查編碼的庫(kù)
url = "http://www.neihan8.com/article/list_5_1.html"
user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'
headers = {'User-Agent': user_agent}
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
html = response.read()
checkCode = chardet.detect(html) # 檢測(cè)網(wǎng)頁(yè)的編碼格式
print('checkCode', checkCode)
#上面那句輸出的結(jié)果checkCode {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}
gbk_html = html.decode(checkCode['encoding']).encode('utf-8')
print(gbk_html)
這上面的代碼運(yùn)行結(jié)果里第一個(gè)title標(biāo)簽如下所示
<title>\xd7\xee\xd3\xd0\xc4\xda\xba\xad\xb5\xc4\xd0\xa6\xbb\xb0_\xc3\xbf
\xc8\xd5\xd2\xbb\xd0\xa6\xca\xd5\xbc\xaf\xd7\xee\xd0\xc2\xb5\xc4\xc4\xda
\xba\xad\xb8\xe3\xd0\xa6\xb6\xce\xd7\xd3\xd0\xa6\xbb\xb0_\xbb\xe7\xb6\xce\
xd7\xd3\xd0\xa6\xbb\xb0_\xc4\xda\xba\xad\xb0\xc9</title>\r\n
回答里的代碼運(yùn)行的結(jié)果第一個(gè)title標(biāo)簽是這樣的
import requests
url = "http://www.neihan8.com/article/list_5_1.html"
print requests.get(url).content.decode('gb2312').encode('utf-8')
<title>\xe6\x9c\x80\xe6\x9c\x89\xe5\x86\x85\xe6\xb6\xb5\xe7\x9a\x84\xe7\
xac\x91\xe8\xaf\x9d_\xe6\xaf\x8f\xe6\x97\xa5\xe4\xb8\x80\xe7\xac\x91\xe6
\x94\xb6\xe9\x9b\x86\xe6\x9c\x80\xe6\x96\xb0\xe7\x9a\x84\xe5\x86\x85\xe6
\xb6\xb5\xe6\x90\x9e\xe7\xac\x91\xe6\xae\xb5\xe5\xad\x90\xe7\xac\x91\xe8
\xaf\x9d_\xe8\x8d\xa4\xe6\xae\xb5\xe5\xad\x90\xe7\xac\x91\xe8\xaf\x9d_\x
e5\x86\x85\xe6\xb6\xb5\xe5\x90\xa7</title>\r\n
是不是我這邊的ide的輸入輸出配置有問(wèn)題?
多謝大家的回答。
不過(guò)問(wèn)題確實(shí)有好幾個(gè)。我把后來(lái)改正過(guò)來(lái)代碼貼一下。
# python3
url = "http://www.neihan8.com/article/list_5_" + str(page) + ".html"
user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'
headers = {'User-Agent': user_agent}
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
html = response.read()
print('python3 response.read()', type(html))
checkCode = chardet.detect(html)
print('checkCode', checkCode)
_html = html.decode(checkCode['encoding'])
print('python3 response.read().decode(gb2312)',type(_html))
# 輸出:
python3 response.read() <class 'bytes'>
checkCode {'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}
python3 response.read().decode(gb2312) <class 'str'>
python3 type(requests.get(url).content) <class 'bytes'>
python3 type(requests.get(url).content.decode('gb2312')) <class 'str'>
下面是python2的代碼:
url = "http://www.neihan8.com/article/list_5_" + str(page) + ".html"
user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'
headers = {'User-Agent': user_agent}
req = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(req)
html = response.read()
print('python2 response.read()', type(html))
checkCode = chardet.detect(html)
print('checkCode', checkCode)
#gbk_html = html.encode(checkCode['encoding']).decode('utf-8')
_html = html.decode('gb2312')#.decode('gb2312')
print('python2 response.read().decode(\'gb2312\')', type(_html))
# 輸出:
('python2 response.read()', <type 'str'>)
('checkCode', {'confidence': 0.99, 'language': 'Chinese', 'encoding': 'GB2312'})
("python2 response.read().decode('gb2312')", <type 'unicode'>)
('python2 type(requests.get(url).content)', <type 'str'>)
("python2 type(requests.get(url).content.decode('gb2312'))", <type 'unicode'>)
雖然2和3返回的源編碼不是同一類型,但只要decode成unicode格式就能print出來(lái)了,總結(jié)起來(lái)還是對(duì)輸入輸出的編碼理解有問(wèn)題。
import requests
url = "http://www.neihan8.com/article/list_5_1.html"
user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'
headers = {'User-Agent': user_agent}
req = requests.get(url, headers=headers)
print(req.encoding)
req.encoding = 'gbk'
print(req.encoding)
print(req.text)
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。