2016-07-30 51 views
1

我對我的第一個項目使用Python從http://www.weather.gov不成功的嘗試後提交表單

我是全新的,以HTTP表單提交提交表單和檢索的天氣數據和工作要求這樣極有可能我我對這一切都錯了。我已經讀過機械化和/或硒對這類工作更有效,但我僅限於學校服務器的這些模塊。

import requests 

r = requests.get('http://www.weather.gov') 

location = raw_input("Enter zipcode: ") 
payload = {'key1' : location} 

q = requests.post('http://forecast.weather.gov/', data = payload) 

print q.text 

我試圖搜索給定的郵政編碼已經失敗,我沒有達到給定郵政編碼的當地天氣。

注意:我也試過使用urllib & urllib2提交表單提交沒有成功。從檢查的頁面看到

import urllib 
import urllib2 

location = raw_input("Enter Zip Code here: ") 

url = 'http://forecast.weather.gov/' 
values = {'inputstring' : location} 
data = urllib.urlencode(values) 

req = urllib2.Request(url, data = data) 
response = urllib2.urlopen(req) 

html = response.read() 
print html 

形式:

<form name="getForecast" id="getForecast" action="http://forecast.weather.gov/zipcity.php" method="get"> 
       <label for="inputstring">Local forecast by <br>"City, St" or ZIP code</label> 
       <input id="inputstring" name="inputstring" type="text" value="Enter location ..." onclick="this.value=''" autocomplete="off"> 
       <input name="btnSearch" id="btnSearch" type="submit" value="Go"> 
       <div id="txtError"> 
        <div id="errorNoResults" style="display:none;">Sorry, the location you searched for was not found. Please try another search.</div> 
        <div id="errorMultipleResults" style="display:none">Multiple locations were found. Please select one of the following:</div> 
        <div id="errorChoices" style="display:none"></div> 
        <input id="btnCloseError" type="button" value="Close" style="display:none"> 
       </div> 
       <div id="txtHelp"><a style="text-decoration: underline;" href="javascript:void(window.open('http://weather.gov/ForecastSearchHelp.html','locsearchhelp','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=500,width=530').focus());">Location Help</a></div> 
      </form> 
<input id="inputstring" name="inputstring" type="text" value="Enter location ..." onclick="this.value=''" autocomplete="off"> 

回答

0

張貼到URL http://forecast.weather.gov/zipcity.php 這就是PHP腳本在於從形式接收後的數據。

+0

謝謝佐羅!我的代碼的urllib版本正常運行。仍然無法讓請求版本正常工作。感謝你的回答! – surfncdog

+0

這是因爲您的請求版本中傳遞的字段是錯誤的。使用'inputstring'而不是'輸入郵編' – zorro