【问题标题】:how do python capture 302 redirect urlpython如何捕获302重定向url
【发布时间】:2011-04-22 16:52:12
【问题描述】:

我尝试从网络获取数据,但页面使用 302 重定向 如何使用 python 获取真实的 url?

【问题讨论】:

    标签: python url redirect capture


    【解决方案1】:

    查看Dive Into Python 系列中的chapter 11.7. Handling redirects。它非常详细地解释了您的整个问题、示例代码和所有内容。

    【讨论】:

      【解决方案2】:

      您目前正在使用什么? urlliburllib2 都应该自动处理它:

      page = urllib.urlopen('http://mrozekma.com/302test.php')
      >>> print page.geturl()   # This will show the redirected-to URL
      http://mrozekma.com/302test.php?success
      >>> print page.readlines()
      ['Success']
      

      【讨论】:

        【解决方案3】:

        如果您使用的是 http.client.HTTPConnection(3.x) 或 httplib.HTTPConnection(2.x),只需获取 Location Header:

        response.getheader('Location')
        

        我知道这至少在 craigslist.org 上有效

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-11
          • 1970-01-01
          • 1970-01-01
          • 2012-03-09
          • 2012-12-18
          • 2011-01-18
          • 2012-11-13
          • 2011-02-07
          相关资源
          最近更新 更多