【问题标题】:urllib.request in Python 2.7Python 2.7 中的 urllib.request
【发布时间】:2011-04-14 07:57:20
【问题描述】:

我可以在 Python 3.1 中使用 urllib.request 模块。但是当我使用 Python 2.7 执行相同的程序时,会出现以下错误:

AttributeError: 'module' 对象没有属性 'request'。

我相信这个错误是因为 Python 2.7 的 urllib 中没有请求模块。因为我需要使用tweepy,所以我必须坚持使用 Python 2.7,因为 tweepy 不支持 Python 3。

那么我如何在 Python 2.7 中使用 urllib.request 模块?

【问题讨论】:

    标签: python twitter tweepy


    【解决方案1】:

    也可以使用six模块为python2和python3编写代码:

    from six.moves import urllib
    # ...
    result = urllib.request.urlopen(url)
    

    【讨论】:

    • 这应该是置顶评论。
    • 这应该是置顶评论
    【解决方案2】:

    对 Python 2 使用 urllib2.urlopen

    【讨论】:

      【解决方案3】:

      看看http://docs.python.org/library/urllib2.html

      urllib2 模块是urllib.request/urllib.error 的前身(在 Python 3.0 中已拆分为这些模块)。

      【讨论】:

        猜你喜欢
        • 2021-04-21
        • 1970-01-01
        • 2018-05-24
        • 2021-05-29
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 2017-12-12
        • 2013-03-27
        相关资源
        最近更新 更多