【问题标题】:Python HTTP Error 505: HTTP Version Not SupportedPython HTTP 错误 505:不支持 HTTP 版本
【发布时间】:2014-05-17 20:48:48
【问题描述】:

我有以下代码:

import re
from re import sub

import cookielib
from cookielib import CookieJar
import urllib2
from urllib2 import urlopen

cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[('user-agent' , 'Safari/7.0.2')]

def check(word):
    try:
        query = "select * from geo.places where text ='"+word+"'"
        sourceCode=opener.open('http://query.yahooapis.com/v1/public/yql?q='+query+'&diagnostics=true').read()
        print sourceCode
    except Exception, e:
        print str(e)
        print 'ERROR IN MAIN TRY'

myStr = ['I','went','to','Boston']
for item in myStr:
        check(item)

我正在尝试查询select * from geo.places where text = 'Boston'(例如)。

我不断收到此错误:

    HTTP Error 505: HTTP Version Not Supported
    ERROR IN MAIN TRY

什么会导致这个错误,我该如何解决?

【问题讨论】:

  • 你能嗅探你对服务器所做的请求并发布它吗?您似乎使用了错误的 HTTP 版本。 urllib2 说它默认使用 HTTP/1.1,这是您尝试联系的服务器使用的版本。这段代码实际上是在发送 HTTP/1.1 吗?

标签: python sql yql


【解决方案1】:

您构建的 URL 不是有效的 URL。你发送的是

GET /v1/public/yql?q=select * from geo.places where text ='I'&diagnostics=true HTTP/1.1
Accept-Encoding: identity
Host: query.yahooapis.com
Connection: close
User-Agent: Safari/7.0.2

网址中不应有空格,例如您必须进行正确的 URL 编码(用“+”等替换空格)。我猜 requests 只是为你修复了错误的 URL。

【讨论】:

    【解决方案2】:

    您的查询之间可能有空格。请求会处理您 url 中的空格,因此您不必处理它。 只需将每个 " " 替换为 "%20" 即可使 URL 正常工作。

    【讨论】:

    • 你是对的。但在 python 中,'%' 是转义字符。我们应该使用“%%20”。
    【解决方案3】:

    不确定,出了什么问题,但是当我尝试使用 requests 库执行相同的操作时,它可以工作:

    >>> import requests
    >>> word = "Boston"
    >>> query = "select * from geo.places where text ='"+word+"'"
    >>> query
    "select * from geo.places where text ='Boston'"
    >>> baseurl = 'http://query.yahooapis.com/v1/public/yql?q='
    >>> url = baseurl + query
    >>> url
    "http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text ='Boston'"
    >>> req = requests.get(url)
    >>> req
    <Response [200]>
    >>> req.text
    u'<?xml version="1.0" encoding="UTF-8"?>\n<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="10" yahoo:created="2014-05-17T21:12:52Z" yahoo:lang="en-US"><results><place xmlns="http://where.yahooapis.com/v1/schema.rng" xml:lang="en-US" yahoo:uri="http://where.yahooapis.com/v1/place/2367105"><woeid>2367105</woeid><placeTypeName code="7">Town</placeTypeName><name>Boston</name><country code="US" type="Country" woeid="23424977">United States</country><admin1 code="US-MA" type="State" woeid="2347580">Massachusetts</admin1><admin2 code="" type="County" woei....
    

    请注意,存在差异,我的代码要简单得多,它不能与 cookie 一起使用,也不会试图伪装成 Safari 浏览器。

    如果您需要在requests 中使用cookies,您会在那里找到很好的支持。

    【讨论】:

      【解决方案4】:

      如其他答案所述,由于空格,您需要对您的网址进行编码。如果使用 python2,调用将是 urllib.quote,对于 python3,调用将是 urllib.parse.quotesafe参数用于编码时忽略字符。

      from urllib import quote
      url = 'http://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text =\'Boston\''
      print(quote(url, safe=':/?*=\''))
      
      # outputs "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%20='Boston'"
      

      【讨论】:

        【解决方案5】:

        使用请求是不错的选择。但我们应该找出原因?

        query = "select * from geo.places where text ='"+word+"'" 您的参数中有一些空间。我们应该对这个空间进行 url 编码。

        你应该将空格转换为'%20',但在python中'%'是特殊字符,你应该使用'%%20'进行转义

        【讨论】:

          猜你喜欢
          • 2015-02-13
          • 1970-01-01
          • 2011-02-14
          • 2011-12-05
          • 1970-01-01
          • 1970-01-01
          • 2013-12-05
          • 1970-01-01
          • 2019-03-10
          相关资源
          最近更新 更多