【问题标题】:Script always gets a 302 response when pulling random pages from Wikipedia从 Wikipedia 中提取随机页面时,脚本总是得到 302 响应
【发布时间】:2011-04-18 19:40:34
【问题描述】:

我可以使用

从维基百科中提取任何页面
import httplib
conn = httplib.HTTPConnection("en.wikipedia.org")
conn.debuglevel = 1
conn.request("GET","/wiki/Normal_Distribution",headers={'User-Agent':'Python httplib'})
r1 = conn.getresponse()
r1.read()

正常反应会是

reply: 'HTTP/1.0 200 OK\r\n'
header: Date: Sun, 03 Apr 2011 23:49:36 GMT
header: Server: Apache
header: Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
header: Content-Language: en
header: Vary: Accept-Encoding,Cookie
header: Last-Modified: Sun, 03 Apr 2011 17:23:50 GMT
header: Content-Length: 263638
header: Content-Type: text/html; charset=UTF-8
header: Age: 1280309
header: X-Cache: HIT from sq77.wikimedia.org
header: X-Cache-Lookup: HIT from sq77.wikimedia.org:3128
header: X-Cache: MISS from sq66.wikimedia.org
header: X-Cache-Lookup: MISS from sq66.wikimedia.org:80
header: Connection: close

但如果我尝试使用 /wiki/Special:Random 拉出一个随机页面,我会收到 302 响应和一个空页面

reply: 'HTTP/1.0 302 Moved Temporarily\r\n'
header: Date: Mon, 18 Apr 2011 19:25:52 GMT
header: Server: Apache
header: Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
header: Vary: Accept-Encoding,Cookie
header: Expires: Thu, 01 Jan 1970 00:00:00 GMT
header: Location: http://en.wikipedia.org/wiki/Tuticorin_Port_Trust
header: Content-Length: 0
header: Content-Type: text/html; charset=utf-8
header: X-Cache: MISS from sq60.wikimedia.org
header: X-Cache-Lookup: MISS from sq60.wikimedia.org:3128
header: X-Cache: MISS from sq62.wikimedia.org
header: X-Cache-Lookup: MISS from sq62.wikimedia.org:80
header: Connection: close

如何获得非空的随机页面?

【问题讨论】:

    标签: python http wikipedia


    【解决方案1】:

    302 是重定向。它在下面的行中告诉你去哪里:

    header: Location: http://en.wikipedia.org/wiki/tuticorin_port_trust 
    

    你只需要跟随重定向。

    【讨论】:

      【解决方案2】:

      当您被重定向时,响应对象的代码将为 302,geturl() 方法将报告重定向 URL。默认情况下,Python 的标准 HTTP 库使处理重定向变得非常简单。帮自己一个忙,不要为这些东西烦恼,并使用第 3 方 mechanize 库,它是 urllib2 的直接替代品。

      使用 mechanize,您的代码将如下所示:

      import httplib
      import mechanize
      
      host = 'en.wikipedia.org'
      path = '/wiki/Special:Random'
      url = 'http://' + host + path # We have to pass a http:// url
      
      # It still uses httplib.HTTPConnection, so we can debug
      httplib.HTTPConnection.debuglevel = 1
      
      request = mechanize.Request(url, headers={'User-Agent': 'Python-mechanize'}) 
      response = mechanize.urlopen(request)
      
      print response.code
      # => 200
      print response.geturl()
      # => 'http://en.wikipedia.org/wiki/Faliszowice,_Lesser_Poland_Voivodeship'
      data = response.read()
      

      【讨论】:

        【解决方案3】:

        HTTP 代码 302 表示您正在被重定向。如果您查看 Location 标头,您将看到应该在哪里发出新请求。然后,您可以向该 URL 发出请求,并有望在该页面上获得 200。

        澄清一下:要求您在别处重试该请求。这就是为什么您的客户在收到 302 时需要发出另一个请求。维基百科的随机页面显然是通过在其数据库中选择一个随机页面来工作的,然后返回一个 302 响应,并将新页面作为 Location 字段。如果您查看其他 302 响应,我相信您会在 Location 字段中看到不同的页面。

        【讨论】:

          【解决方案4】:

          查看位置标头:

          标题:位置:http://en.wikipedia.org/wiki/Tuticorin_Port_Trust

          它说您应该重定向到该页面。读取该标头并对该页面发出另一个请求。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-08-31
            • 1970-01-01
            • 1970-01-01
            • 2017-08-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多