Skip to main content
 首页 » 编程设计

Python HTTP 错误 505 : HTTP Version Not Supported

2025年05月04日26lyj

我有以下代码:

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 

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

请您参考如下方法:

您构造的 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 中不应有空格,例如您必须进行正确的 URL 编码(用“+”等替换空格)。我想 requests 只是为您修复了错误的 URL。