日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫文字全是乱码_pythone爬虫编码自适应 解决网页乱码

發布時間:2024/1/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫文字全是乱码_pythone爬虫编码自适应 解决网页乱码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該樓層疑似違規已被系統折疊?隱藏此樓查看此樓

#coding=utf-8

import chardet #字符集檢測

import urllib.parse

import urllib.request

import re

import ssl

#跳過 SSL證書

ssl._create_default_https_context=ssl._create_unverified_context

rr = re.compile(r"\bcharset[=:\"\s]{1,3}([-_A-Z0-9]+)",re.I)

def getCode(string):

p = rr.findall(string)

if len(p)>0:

print(u'編碼方式: ' + p[0])

return p[0]

print(u'沒找到編碼方式')

return ''

#getCode(r'iiifjjd charset:" utf_8iidi-oo">')

def getHtml(url):

headers={

"User-Agent": 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',

'Referer': url

}

values = {

'name': 'hao_hao',

'ie': 'utf-8'

}

data = urllib.parse.urlencode(values)

req = urllib.request.Request(url=url+'?'+data, headers=headers)

#req = urllib.request.Request(url+'?'+data)

response = urllib.request.urlopen(req)

#1 從響應頭中找編碼方式

page = getCode(response.headers['Content-Type'])

#2 從網頁源代碼中找編碼方式

if page == '':

for line in response.readlines():

page = getCode(line.decode())

if page !='': break

the_page = response.read()

#3 chardet字符集檢測 進行內容分析. https://mm.taobao.com/search_tstar_model.html GBK 識別成 GB2312 所以不好用. 前兩個方法都不行再用

if page =='':

chardit1 = chardet.detect(the_page)

page = chardit1['encoding']

print(u'chardet字符集檢測\r\n編碼方式: ' + page)

#打印響應頭數據.

print(response.headers)

#需要時關閉連接

#response.close()

#都找不到編碼方式

if page =='': return ''

return the_page.decode(page) #解碼.

#return the_page.decode(page).encode('utf-8')

print ('===============================================')

#gbk

html = getHtml("https://mm.tao[請把這幾個字刪掉]bao.com/search_tstar_model.html")

print (html)

print ('===============================================')

#utf-8

html = getHtml("http://kyfw.123[請把這幾個字刪抻]06.cn/otn/leftTicket/init")

print (html)

print ('===============================================')

總結

以上是生活随笔為你收集整理的python爬虫文字全是乱码_pythone爬虫编码自适应 解决网页乱码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。