Search code examples
pythonformspython-requestsurllib2urllib

Unsuccessful attempts to post request for form submission


I am working on my first project using python to submit a form and retrieve weather data from http://www.weather.gov

I am brand new to HTTP form submission and as such it is highly possible I am going about this all wrong. I've read that mechanize and/or selenium are more efficient for this type of job, but I am limited to these modules by my school's server.

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

My attempts to search a given zipcode have been unsuccessful, I am not reaching the local weather for the given zipcode.

Note: I have also tried this form submission using urllib & urllib2 without success.

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 as seen from inspecting the page:

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

Solution

  • Post to the URL http://forecast.weather.gov/zipcity.php That is where the php script lies for receiving post data from the form.