我有以下代码:
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'(例如)。
select * from geo.places where text = 'Boston'
我不断收到此错误:
HTTP Error 505: HTTP Version Not Supported ERROR IN MAIN TRY
什么会导致此错误,我该如何解决?
不知道出了什么问题,但是当我尝试使用requests库执行相同的操作时,它会起作用:
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浏览器。
如果您需要将Cookie与一起使用requests,则可以在其中找到很好的支持。