我沒有改變我的代碼,但現在當我嘗試運行它,我得到是工作過,但沒有得到urllib.error.HTTPError:HTTP錯誤400:錯誤的請求
urllib.error.HTTPError: HTTP Error 400: Bad Request
在此線
response = urlopen(url).read().decode('utf8')
URL是
url = 'http://api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/{}/{}.json'.format(state.strip(), city)
我是否應該等到稍後再試一次,因爲它是工作,直到剛剛一個小時前。
編輯:
我使用Python 3.5和已更改爲
url = quote("http://api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/{}/{}.json").format(state.strip(), city)
這將導致URL
http%3A//api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/%7B%7D/%7B%7D.json
和錯誤消息
ValueError: unknown url type: 'http%3A//api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/%7B%7D/%7B%7D.json'
編輯2:
我用
url = 'api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/{}/{}.json'.format(state.strip(), city)
print (url)
url1 = quote(url)
url2 = 'http://'+ url1
要編碼的空間,但我仍然得到400錯誤。我會等到明天再試一次,因爲或許我的訪問存在問題。
編輯: 它正在與沒有空間的城市合作。所以我會繼續努力讓自己的空間工作。謝謝你的幫助。
我認爲你的'url'變量的格式不正確的可能性更大。這是[HTTP錯誤400的含義](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes):服務收到請求但無法理解。在調用urlopen()之前,請打印'url'來驗證狀態和城市是否按照您的預期進行插值。 –
我已經這樣做了,我得到了這個http://api.wunderground.com/api/f8cb961c53baad52/geolookup/conditions/q/ID/Boise City.json當我去那個網址我自己打開沒有問題。但現在任何城市,我一直在嘗試我的文件不起作用。 – Sam
你應該使用'urllib.quote()'來編碼你的變量,因爲URL中的空格會使urlopen搞亂。請參閱http://stackoverflow.com/questions/21735801/how-to-send-a-urllib2-request-with-added-white-spaces –