欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3的UnicodeDecodeError解決方法

 更新時間:2019年12月20日 10:22:00   作者:__rookie  
這篇文章主要介紹了python3的UnicodeDecodeError解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

爬蟲部分解碼異常

response.content.decode() # 默認(rèn)使用 utf-8 出現(xiàn)解碼異常

以下是設(shè)計(jì)的通用解碼

通過 text 獲取編碼

# 通過 text 獲取編碼
import requests
from lxml import etree


def public_decode():
 headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
 }
 response = requests.get('https://blog.csdn.net/a13951206104', headers=headers)
 html = etree.HTML(response.text) # response.text 能自動獲取編碼, 大多亂碼
 _charset = html.xpath('//@charset') or []
 if _charset:
  encode_content = response.content.decode(_charset[0].strip().lower(),
             errors='replace') # 如果設(shè)置為replace,則會用?取代非法字符;
  return {'response_text': encode_content, "response_obj": response}
 for _charset_ in ['utf-8', 'gbk', 'gb2312'] # 國內(nèi)主要這3種:
  if '�' not in response.content.decode(_charset_, errors='replace'):
   return {'response_text': response.content.decode(_charset_, errors='replace'),
     "response_obj": response}
  else:
   # 默認(rèn)還得是 utf-8
   return {'response_text': response.content.decode('utf-8', errors='replace'),
     "response_obj": response}

通過數(shù)據(jù) 來解編碼(推薦)

def public_decode(response):
 headers = {
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
 }
 response = requests.get('https://blog.csdn.net/a13951206104', headers=headers)
 html = etree.HTML(response.text)
 # 不希望抓下來的數(shù)據(jù)中有非法字符
 item = dict()
 result = None
 for _charset_ in ['utf-8', 'gbk', 'gb2312']:
  if response:
   result = response.content.decode(_charset_, errors='replace')
   item['content'] = html.xpath('//*[@id="content"]')
   if '�' not in result['content'].strip():
    result =response.content.decode(_charset_, errors='replace')
    break
 if not result:
  # 默認(rèn) utf-8
  result = response.content.decode(_charset_, errors='replace')
 

errors=‘replace' 使解碼不報(bào)異常, 然后把幾個常用的編碼一個個試下, 最后要看落下來的數(shù)據(jù), 所以最好拿數(shù)據(jù) 去獲取合適的編碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python用list或dict字段模式讀取文件的方法

    Python用list或dict字段模式讀取文件的方法

    這篇文章主要給大家介紹了Python利用list字段模式或者dict字段模式讀取文件的方法,文中給出了詳細(xì)的介紹和示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友可以跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-01-01
  • Python繪圖之實(shí)現(xiàn)繪制極坐標(biāo)圖像

    Python繪圖之實(shí)現(xiàn)繪制極坐標(biāo)圖像

    這篇文章主要介紹了如何利用python繪制極坐標(biāo)圖像,文中的示例代碼講解詳細(xì),具有一定的的參考價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • Python numpy多維數(shù)組實(shí)現(xiàn)原理詳解

    Python numpy多維數(shù)組實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了python numpy多維數(shù)組實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 如何理解Python中包的引入

    如何理解Python中包的引入

    在本篇文章里小編給各位分享的是一篇關(guān)于Python中包的引入詳解內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。
    2020-05-05
  • python使用PyGame模塊播放聲音的方法

    python使用PyGame模塊播放聲音的方法

    這篇文章主要介紹了python使用PyGame模塊播放聲音的方法,實(shí)例分析了PyGame模塊的使用技巧,需要的朋友可以參考下
    2015-05-05
  • 最新評論