【问题标题】:Python 'utf8' codec can't decode byte 0xc3 in position 72: invalid continuation byte [duplicate]Python'utf8'编解码器无法解码位置72的字节0xc3:无效的继续字节[重复]
【发布时间】:2016-07-10 03:50:51
【问题描述】:

我正在从 google.com 抓取一个特定的 url,但我得到了一些错误

'utf8' codec can't decode byte 0xc3 in position 72: invalid continuation byte

代码:

import re
import os
import MySQLdb
import codecs
import requests
import base64
import random
import gzip
import time
from multiprocessing.pool import Pool
import datetime
import time

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def proxy_mesh():
    while True:
        try: 

            data = requests.get('google.com')

            print data.text.encode('utf-8')
        except Exception, e:
            print e
            print "Trying again"
            time.sleep(3)
proxy_mesh()

什么是 FIX 以及如何克服这个错误?

【问题讨论】:

  • 换句话说,您尝试使用utf-8 进行解码,而编码方式不同。
  • 可以给回溯吗?这可能在多个地方隐含地发生。
  • @Mounarajan 正如我提供的链接中所建议的那样,您需要使用不同的编码。如果没有更多信息,无法告诉您是哪一个。

标签: python unicode python-unicode


【解决方案1】:

保持简单并且有效。数据已被requests 模块解码。

import requests
data = requests.get('https://www.whoisxmlapi.com/whoisserver/WhoisService?domainName=http://N%E2%94%9CO-RESPONDER@MERCAOLIVRE.COM&outputFormat=json')
print data.text

由于它是 JSON 响应,您可能还想对其进行处理:

import json
print json.loads(data.text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2019-08-07
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多