Python HTTP Error 505: HTTP Version Not Supported

跟風遠走 提交于 2019-12-06 21:13:59

Not sure, what is going wrong, but when I try to do the same action using requests library, it works:

>>> 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....

Note, that there are differences, my code is much simpler, it does not work with cookies and it does not try to pretend Safari browser.

If you need to use cookies with requests, you will find very good support for it there.

The URL you construct is not a valid URL. What you send is

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

There should be no spaces inside the URL, e.g. you have to do proper URL encoding (replace space with '+' etc). I guess requests just fixes the bad URL for you.

Your query might have blank spaces in between. Requests take care of the white spaces in your url and hence you don't have to take care of it. Just replace each " " by "%20" to make the url work.

As stated in other answers, you need to encode your url due to the white space. The call would be urllib.quote if using python2 or urllib.parse.quote for python3. The safe parameter is used to ignore characters when encoding.

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'"

Use requests is good choose. but we should found out why?

query = "select * from geo.places where text ='"+word+"'" There are some space in your paramter.we should be url encode this space.

you should convert the spaces to '%20', but in python the '%' is special char, you should be escaped use '%%20'

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!