2016-07-30 7 views
1

ich an meinem ersten Projekt arbeite Python mit einem Formular abzuschicken und Wetterdaten aus http://www.weather.govErfolglose Versuche Anfrage für Formularübermittlung zu schreiben

Ich bin ganz neu in HTTP-Übertragungs und als solche ist sie sehr gut möglich, ich abrufen Ich mache das alles falsch. Ich habe gelesen, dass Mechanisierung und/oder Selen für diese Art von Arbeit effizienter sind, aber ich bin auf diese Module vom Server meiner Schule beschränkt.

Meine Versuche, eine Postleitzahl zu suchen, waren nicht erfolgreich.

Hinweis: Ich habe auch versucht, dieses Formular Einreichung mit urllib & Urllib2 ohne Erfolg.

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 

Formular mit Wirkung vom Inspizieren der Seite gesehen:

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

Antwort

0

Beitrag an die URL http://forecast.weather.gov/zipcity.php das ist, wo der PHP-Skript liegt aus dem Formular Post zum Empfangen von Daten.

+0

Danke Zorro! Ich habe die Urlib-Version meines Codes ordnungsgemäß ausgeführt. Trotzdem kann die Anfrageversion nicht funktionieren. Danke für die Antwort! – surfncdog

+0

Dies liegt daran, dass das übergebene Feld in Ihrer Anfrageversion falsch ist. Verwenden Sie 'inputstring' anstelle von 'Postleitzahl eingeben' – zorro

Verwandte Themen