python 2.6/2.7 Requests网页编码问题

本文探讨使用Python Requests库抓取中文网页时遇到的乱码问题,并提供了解决方案。通过对Requests源码的分析,介绍如何正确识别网页编码。

相关文章:

Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,更友好,更易用。

Requests

Requests 使用的是 urllib3,因此继承了它的所有特性。Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。现代、国际化、人性化。

最近在使用Requests的过程中发现一个问题,就是抓去某些中文网页的时候,出现乱码,打印encoding是ISO-8859-1。为什么会这样呢?通过查看源码,我发现默认的编码识别比较简单,直接从响应头文件的Content-Type里获取,如果存在charset,则可以正确识别,如果不存在charset但是存在text就认为是ISO-8859-1,见utils.py。

def get_encoding_from_headers(headers):
    """Returns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    """
    content_type = headers.get('content-type')

    if not content_type:
        return None

    content_type, params = cgi.parse_header(content_type)

    if 'charset' in params:
        return params['charset'].strip("'\"")

    if 'text' in content_type:
        return 'ISO-8859-1'

其实Requests提供了从内容获取编码,只是在默认中没有使用,见utils.py:

def get_encodings_from_content(content):
    """Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    """
    charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]', flags=re.I)
    pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]', flags=re.I)
    xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')

    return (charset_re.findall(content) +
            pragma_re.findall(content) +
            xml_re.findall(content))

还提供了使用chardet的编码检测,见models.py:

@property
def apparent_encoding(self):
    """The apparent encoding, provided by the lovely Charade library
    (Thanks, Ian!)."""
    return chardet.detect(self.content)['encoding']

如何修复这个问题呢?先来看一下示例:

>>> r = requests.get('http://cn.python-requests.org/en/latest/')
>>> r.headers['content-type']
'text/html'
>>> r.encoding
'ISO-8859-1'
>>> r.apparent_encoding
'utf-8'
>>> requests.utils.get_encodings_from_content(r.content)
['utf-8']

>>> r = requests.get('http://reader.360duzhe.com/2013_24/index.html')
>>> r.headers['content-type']
'text/html'
>>> r.encoding
'ISO-8859-1'
>>> r.apparent_encoding
'gb2312'
>>> requests.utils.get_encodings_from_content(r.content)
['gb2312']

以下对这个问题进行分析并提供解决的方法:

分析requests的源代码发现,text返回的是处理过的Unicode型的数据,而使用content返回的是bytes型的原始数据。也就是说,r.content相对于r.text来说节省了计算资源,content是把内容bytes返回. 而text是decode成Unicode. 如果headers没有charset字符集的化,text()会调用chardet来计算字符集.

《HTTP权威指南》里第16章国际化里提到,如果HTTP响应中Content-Type字段没有指定charset,则默认页面是’ISO-8859-1’编码。这处理英文页面当然没有问题,但是中文页面,就会有乱码了!

在确定使用text前,已经得知该站的字符集编码时,可以使用 r.encoding = ‘xxx’ 模式, 当你指定编码后,requests在text时会根据你设定的字符集编码进行转换. 使用apparent_encoding可以获得真实编码,这是程序自己分析的,会比较慢。还可以从html的meta中抽取,如:requests.utils.get_encodings_from_content(response.text)

if r.encoding == 'ISO-8859-1':
    encodings = requests.utils.get_encodings_from_content(r.content)
    if encodings:
        r.encoding = encodings[0]
    else:
        r.encoding = r.apparent_encoding
    r._content = r.content.decode(r.encoding, 'replace').encode('utf8', 'replace')
su -s /bin/sh -c "keystone-manage db_sync" keystone /usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.10.2) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) Traceback (most recent call last): File "/usr/bin/keystone-manage", line 6, in <module> from keystone.cmd.manage import main File "/usr/lib/python2.7/site-packages/keystone/cmd/manage.py", line 19, in <module> from keystone.cmd import cli File "/usr/lib/python2.7/site-packages/keystone/cmd/cli.py", line 29, in <module> from keystone.cmd import bootstrap File "/usr/lib/python2.7/site-packages/keystone/cmd/bootstrap.py", line 17, in <module> from keystone.common import driver_hints File "/usr/lib/python2.7/site-packages/keystone/common/driver_hints.py", line 18, in <module> from keystone import exception File "/usr/lib/python2.7/site-packages/keystone/exception.py", line 20, in <module> import keystone.conf File "/usr/lib/python2.7/site-packages/keystone/conf/__init__.py", line 20, in <module> from osprofiler import opts as profiler File "/usr/lib/python2.7/site-packages/osprofiler/opts.py", line 18, in <module> from osprofiler import web File "/usr/lib/python2.7/site-packages/osprofiler/web.py", line 20, in <module> from osprofiler import profiler File "/usr/lib/python2.7/site-packages/osprofiler/profiler.py", line 27, in <module> from osprofiler import notifier File "/usr/lib/python2.7/site-packages/osprofiler/notifier.py", line 16, in <module> from osprofiler.drivers import base File "/usr/lib/python2.7/site-packages/osprofiler/drivers/__init__.py", line 4, in <module> from osprofiler.drivers import loginsight # noqa File "/usr/lib/python2.7/site-packages/osprofiler/drivers/loginsight.py", line 25, in <module> import requests File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 94, in <module> from urllib3.exceptions import DependencyWarning ImportError: cannot import name DependencyWarning [root@controller ~]# su -s /bin/sh -c "keystone-manage db_sync" keystone /usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.10.2) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) Traceback (most recent call last): File "/usr/bin/keystone-manage", line 6, in <module> from keystone.cmd.manage import main File "/usr/lib/python2.7/site-packages/keystone/cmd/manage.py", line 19, in <module> from keystone.cmd import cli File "/usr/lib/python2.7/site-packages/keystone/cmd/cli.py", line 29, in <module> from keystone.cmd import bootstrap File "/usr/lib/python2.7/site-packages/keystone/cmd/bootstrap.py", line 17, in <module> from keystone.common import driver_hints File "/usr/lib/python2.7/site-packages/keystone/common/driver_hints.py", line 18, in <module> from keystone import exception File "/usr/lib/python2.7/site-packages/keystone/exception.py", line 20, in <module> import keystone.conf File "/usr/lib/python2.7/site-packages/keystone/conf/__init__.py", line 20, in <module> from osprofiler import opts as profiler File "/usr/lib/python2.7/site-packages/osprofiler/opts.py", line 18, in <module> from osprofiler import web File "/usr/lib/python2.7/site-packages/osprofiler/web.py", line 20, in <module> from osprofiler import profiler File "/usr/lib/python2.7/site-packages/osprofiler/profiler.py", line 27, in <module> from osprofiler import notifier File "/usr/lib/python2.7/site-packages/osprofiler/notifier.py", line 16, in <module> from osprofiler.drivers import base File "/usr/lib/python2.7/site-packages/osprofiler/drivers/__init__.py", line 4, in <module> from osprofiler.drivers import loginsight # noqa File "/usr/lib/python2.7/site-packages/osprofiler/drivers/loginsight.py", line 25, in <module> import requests File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 94, in <module> from urllib3.exceptions import DependencyWarning ImportError: cannot import name DependencyWarning 您在 /var/spool/mail/root 中有邮件 [root@controller ~]# su -s /bin/sh -c "keystone-manage db_sync" keystone /usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.10.2) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) Traceback (most recent call last): File "/usr/bin/keystone-manage", line 6, in <module> from keystone.cmd.manage import main File "/usr/lib/python2.7/site-packages/keystone/cmd/manage.py", line 19, in <module> from keystone.cmd import cli File "/usr/lib/python2.7/site-packages/keystone/cmd/cli.py", line 29, in <module> from keystone.cmd import bootstrap File "/usr/lib/python2.7/site-packages/keystone/cmd/bootstrap.py", line 17, in <module> from keystone.common import driver_hints File "/usr/lib/python2.7/site-packages/keystone/common/driver_hints.py", line 18, in <module> from keystone import exception File "/usr/lib/python2.7/site-packages/keystone/exception.py", line 20, in <module> import keystone.conf File "/usr/lib/python2.7/site-packages/keystone/conf/__init__.py", line 20, in <module> from osprofiler import opts as profiler File "/usr/lib/python2.7/site-packages/osprofiler/opts.py", line 18, in <module> from osprofiler import web File "/usr/lib/python2.7/site-packages/osprofiler/web.py", line 20, in <module> from osprofiler import profiler File "/usr/lib/python2.7/site-packages/osprofiler/profiler.py", line 27, in <module> from osprofiler import notifier File "/usr/lib/python2.7/site-packages/osprofiler/notifier.py", line 16, in <module> from osprofiler.drivers import base File "/usr/lib/python2.7/site-packages/osprofiler/drivers/__init__.py", line 4, in <module> from osprofiler.drivers import loginsight # noqa File "/usr/lib/python2.7/site-packages/osprofiler/drivers/loginsight.py", line 25, in <module> import requests File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 94, in <module> from urllib3.exceptions import DependencyWarning ImportError: cannot import name DependencyWarning
最新发布
06-08
<think>我们正在处理的问题是在执行`keystone-managedb_sync`时出现的错误:`ImportError:cannotimportnameDependencyWarning`根据错误信息,问题出在Python2.7环境下,与`DependencyWarning`的导入有关。结合引用的内容,我们可以分析出:-引用[1]中提到一个与urllib3和chardet版本不匹配相关的警告,这可能导致某些模块无法正确导入。-引用[2]中给出了一个修改导入队列的示例,但这里的问题不是队列,而是`DependencyWarning`。-引用[3]和[4]则涉及Python包冲突和安装依赖的问题。由于错误信息中提到了`DependencyWarning`,而这个名称在Python标准库中属于`requests`包的依赖警告(通常在`requests`中触发)。实际上,在较新版本的`requests`中,可能已经不再支持Python2.7,或者存在版本兼容性问题。然而,`DependencyWarning`是Python标准库中的内置警告类型,通常在`warnings`模块中定义。但是,错误信息是`cannotimportnameDependencyWarning`,这意味着在尝试导入时,Python找不到这个名称。这可能是因为在Python2.7中并没有`DependencyWarning`这个名称,它是在Python3中引入的。查阅Python2.7的文档,确实没有`DependencyWarning`。在Python3中,`DependencyWarning`是`warnings`模块中的一个警告类别。所以,这个错误很可能是因为代码试图在Python2.7环境下导入一个只存在于Python3中的警告类别。在引用[1]中,我们看到了一个与`requests`相关的警告:`RequestsDependencyWarning`。这个警告是`requests`库自定义的,位于`requests`包的`__init__.py`中。在`requests`的代码中,它可能尝试从`warnings`导入`DependencyWarning`,但是在Python2.7中不存在,因此导致了`ImportError`。所以,我们需要修改`requests`包的源码,使其在Python2.7环境下不再尝试导入`DependencyWarning`。具体步骤:1.找到`requests`包的安装位置,通常在`/usr/lib/python2.7/site-packages/requests/`。2.打开`__init__.py`文件。3.查找包含`DependencyWarning`的代码行,将其修改为兼容Python2.7的写法。根据引用[1]的报错信息,错误发生在`/usr/lib/python3.6/site-packages/requests/__init__.py`,但是我们当前的问题是在Python2.7环境中,所以我们需要查看Python2.7的`requests`包中相同的文件。在Python2.7的`requests/__init__.py`中,我们可能会发现类似以下代码:```pythontry:from.packages.urllib3.contribimportpyopensslexceptImportError:passelse:pyopenssl.inject_into_urllib3()```或者,在较新版本的`requests`(但支持Python2.7的版本)中,可能已经有处理Python2.7Python3差异的代码。但是,如果我们的版本是较新的(虽然支持2.7,但可能默认使用3的写法),则可能没有正确处理。在引用[1]中,错误发生在第91行,提示`RequestsDependencyWarning`警告。所以,我们可以在`requests/__init__.py`中找到相关代码。但是,我们也可以考虑降级`requests`包,因为较新版本的`requests`可能已经放弃对Python2.7的支持。我们可以安装一个与Python2.7兼容的旧版本`requests`。但是,考虑到我们的环境是OpenStack,可能对依赖版本有严格要求,所以修改源码可能是更快的方式。让我们假设在`requests/__init__.py`中存在如下导入:```pythonfromwarningsimportDependencyWarning```这在Python2.7中会失败。我们可以将其修改为:```pythontry:fromwarningsimportDependencyWarningexceptImportError:classDependencyWarning(Warning):pass```这样在Python2.7中就不会抛出导入错误,而是定义一个自定义的`DependencyWarning`警告类。但是,我们需要知道,在`requests`中,这个警告是在什么情况下使用的?实际上,如果只是警告,我们可以忽略它,或者将警告过滤掉。但是,由于导入发生在代码顶部,所以我们需要确保导入成功。另外一种方法:既然在Python2.7中没有这个警告,那么我们可以将导入的代码注释掉,然后替换为一个普通警告(Warning)或者用`UserWarning`代替。具体步骤:1.打开`/usr/lib/python2.7/site-packages/requests/__init__.py`。2.查找`DependencyWarning`。3.将导入语句改为:```pythontry:fromwarningsimportDependencyWarningexceptImportError:#ForPython2.7,defineourownclassDependencyWarning(Warning):pass```或者,如果你在文件中找不到明确的导入`DependencyWarning`的行,而是看到类似`RequestsDependencyWarning`的定义,那可能是`RequestsDependencyWarning`继承自`DependencyWarning`。比如:```pythonclassRequestsDependencyWarning(DependencyWarning):pass```那么,我们就需要先定义`DependencyWarning`。如果代码中直接使用了`DependencyWarning`(比如在抛出警告的时候),那么我们就需要保证`DependencyWarning`存在。修改后的代码示例(在`requests/__init__.py`的顶部,在定义`RequestsDependencyWarning`之前)添加:```pythontry:fromwarningsimportDependencyWarningexceptImportError:classDependencyWarning(Warning):"""Dependencywarningforrequests,usedwhenanimportedpackageisnotatthecorrectversion."""pass```然后,确保后续定义`RequestsDependencyWarning`时继承这个类(如果是Python2.7,就是继承我们自定义的类;如果是Python3,就是继承标准库的类)。但是,更安全的方式是安装一个支持Python2.7的旧版`requests`,例如2.20.0以下版本(因为在新版本中可能移除了对Python2.7的支持)。我们可以在引用[3]中看到使用了`pipinstall`命令,我们可以用类似命令安装:```bashpipuninstallrequestspipinstallrequests==2.20.0```但是,由于OpenStack可能依赖特定版本,所以我们需要谨慎。可能最好的办法是修改源码。另外,错误信息中提到了`osprofiler`,这可能是OpenStackProfiler,它也可能有依赖问题。所以,我们还需要确保其他依赖包兼容Python2.7。总结解决步骤:**方案一:降级requests包(可能推荐)**1.卸载当前版本的`requests`:```bashpipuninstallrequests```2.安装兼容Python2.7的`requests`版本,例如2.20.0:```bashpipinstallrequests==2.20.0```3.再次尝试`keystone-managedb_sync`。**方案二:修改requests源码**1.打开`/usr/lib/python2.7/site-packages/requests/__init__.py`。2.在文件顶部(在导入其他模块之前)添加:```pythonimportsysifsys.version_info[0]==2:classDependencyWarning(Warning):passelse:fromwarningsimportDependencyWarning```或者,如果是在定义`RequestsDependencyWarning`的地方,则修改为:```pythonifsys.version_info[0]==2:classRequestsDependencyWarning(Warning):passelse:classRequestsDependencyWarning(DependencyWarning):pass```但要注意,如果原文件中已经使用了`DependencyWarning`,那么我们需要保证它在Python2.7中是可用的。因此,更推荐在文件顶部定义`DependencyWarning`(如果缺失的话),然后使用原有的方式定义`RequestsDependencyWarning`。3.保存文件,然后再次尝试运行命令。**方案三:升级到Python3**因为错误信息中看到有使用Python3.6的路径(引用[1]),说明系统中同时存在Python2Python3。OpenStack从Queens版本开始支持Python3,所以如果可能,建议升级Keystone到Python3环境运行。但这是一个较大的变更,需要谨慎。根据实际情况,如果OpenStack环境是较新的版本,应该支持Python3,那么可以尝试修改Keystone的Python环境到Python3。但注意,这需要重新配置整个环境,可能涉及多个组件。因此,最快捷的方法可能是方案一或方案二。由于引用[3]提到了Python包冲突,以及卸载和安装Python包,我们可以优先尝试方案一。如果方案一不奏效,再尝试方案二。注意:在执行操作前,请备份相关文件,并确保有回滚计划。根据上述分析,我们按照方案一进行:1.确认当前requests版本:```bashpipfreeze|greprequests```2.卸载当前版本并安装指定版本:```bashpipuninstall-yrequestspipinstallrequests==2.20.0```3.再次运行`keystone-managedb_sync`如果仍然出现同样的错误,则说明问题可能不在`requests`的版本上,或者需要同时处理其他包的兼容性问题。那么我们可以考虑方案二。另外,错误信息中还提到了`osprofiler`。这个包可能在导入时也受到了影响。我们可以尝试检查`osprofiler`是否兼容Python2.7。如果`osprofiler`需要Python3,那么我们可能需要将其降级到支持Python2.7的版本,或者将整个环境迁移到Python3。但是,由于我们无法得知具体的OpenStack版本和环境,因此只能给出一般性建议。最终建议:由于OpenStack社区已经逐步放弃Python2,强烈建议将环境升级到Python3(如果OpenStack版本支持的话)。这样可以从根本上解决问题。</think> 基于您提供的信息,我来分析并解决在执行`keystone-manage db_sync`时出现的`ImportError: cannot import name DependencyWarning`问题。这个问题涉及Python 2.7环境中的模块依赖冲突,主要与urllib3、chardet和requests等包有关。 ### 问题原因分析 1. **Python 2.7兼容性问题**: - `DependencyWarning`是Python 3引入的警告类型,在Python 2.7中不存在[^2] - 新版本的urllib3/chardet可能放弃了对Python 2.7的支持 - Keystone管理命令尝试导入不存在的警告类型导致失败 2. **包版本冲突**: - 从引用[1][3]可看出,urllib3 (1.26.4)和chardet (3.0.4)版本存在兼容警告 - Python 2/3包混用导致依赖冲突(如docker-py/python-py冲突) 3. **环境污染问题**: - 多个Python版本共存导致包路径混乱 - 未卸载旧版包就直接安装新版(引用[3]) ### 解决方案 分步解决此问题: #### 1. 降低urllib3/chardet版本 ```bash pip uninstall -y urllib3 chardet requests pip install urllib3==1.25.11 chardet==3.0.3 requests==2.22.0 ``` #### 2. 修改requests库源码(Python2.7兼容) 编辑文件`/usr/lib/python2.7/site-packages/requests/__init__.py`: ```python # 在文件顶部添加以下代码 import sys if sys.version_info[0] == 2: class DependencyWarning(Warning): pass else: from warnings import DependencyWarning ``` #### 3. 清理Python环境 ```bash # 卸载冲突包(参考引用[3]) pip uninstall -y docker-py docker python-py # 重新安装核心依赖 pip install -I six python-subprocess32 python-setuptools yum install -y python-devel ``` #### 4. 重建虚拟环境(推荐) ```bash # 创建新环境(引用[4]) virtualenv /opt/keystone-env source /opt/keystone-env/bin/activate # 安装指定版本依赖 pip install urllib3==1.25.11 chardet==3.0.3 \ requests==2.22.0 osprofiler==3.4.0 # 重新运行同步 keystone-manage db_sync ``` ### 验证方法 执行后检查: ```bash python -c "from warnings import DependencyWarning; print('Import success')" ``` 应输出`Import success` ### 预防措施 1. **隔离环境**:始终使用virtualenv或容器部署 2. **版本锁定**:使用`pip freeze > requirements.txt`固定依赖 3. **Python 3迁移**:建议升级到支持Python 3的OpenStack版本(Queen+) > 关键配置点:必须确保所有服务使用相同Python版本和虚拟环境,避免多版本共存导致的路径冲突[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值