HTTP Error:HTTP错误400:错误的请求urllib2

2024-04-20 10:42:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python初学者。我是Easy API项目(http://gcdc2013-easyapisproject.appspot.com)的开发人员,正在使用我的项目执行天气API的Python实现。访问http://gcdc2013-easyapisproject.appspot.com/APIs_Doc.html查看天气API。下面是我的实现,但它返回HTTPError: HTTP Error 400: Bad request错误。

import urllib2

def celsius(a):
    responsex = urllib2.urlopen('http://gcdc2013-easyapisproject.appspot.com/unitconversion?q='+a+' in celsius')
    htmlx = responsex.read()
    responsex.close()
    htmlx = html[1:] #remove first {
    htmlx = html[:-1] #remove last }
    htmlx = html.split('}{') #split and put each resutls to array
    return str(htmlx[1]);

print "Enter a city name:",
q = raw_input() #get word from user
response = urllib2.urlopen('http://gcdc2013-easyapisproject.appspot.com/weather?q='+q)
html = response.read()
response.close()
html = html[1:] #remove first {
html = html[:-1] #remove last }
html = html.split('}{') #split and put each resutls to array
print "Today weather is " + html[1]
print "Temperature is " + html[3]
print "Temperature is " + celsius(html[3])

请帮帮我。。


Tags: comapihttpresponsehtmlurllib2removesplit