tmthyjames
tmthyjames

Reputation: 1605

Making a post request when there are no attributes in the form element?

I'm looping through zip codes and retrieving information from this site http://www.airnow.gov/index.cfm?action=school_flag_program.sfp_createwidget

Here's the form and input elements:

<form name="groovyform">
<input type="text" name="Title" id="Title" size="20" maxlength="20" />
<input type="text" name="Zipcode" id="Zipcode" size="10" maxlength="10" />

My question is how do I make a post request if there are no attributes in the form element (such as action or method)?

My code (I've tried request.get with the params argument, and request.post with the data argument):

url = 'http://www.airnow.gov/index.cfm?action=school_flag_program.sfp_createwidget'
data_to_send = {'zipcode ':'37217',
                'Title': 'ph'}
response = requests.get(url, params=data_to_send)
contents = response.text
print contents

just returns the HTML of the url but I want the HTML of the page I get when I post the data. In other words, I don't think request.get is submitting my data and I think it has something to do with there not being an action or method attribute.

Enlighten me!

Thanks!

Upvotes: 0

Views: 79

Answers (3)

tmthyjames
tmthyjames

Reputation: 1605

Ok, so like Barmar stated, the <form> I posted isn't supposed to be submitted. The form I was supposed to be filling out (top of the page) contained the following:

<form name="frmZipSearch" method="get" style="width:178px; float:left;">
 Zip Code:
<input name="zipcode" type="text" size="5"  maxlength="5" height="20">

Now my code works.

url = 'http://www.airnow.gov/index.cfm?action=airnow.local_city&zipcode=37217&submit=Go'
data_to_send = {'zipcode':'37217'}
response = requests.get(url, data=data_to_send)
contents = response.text
print contents

Thanks, Barmar, for directing me to the right path.

Upvotes: 0

Barmar
Barmar

Reputation: 781731

That form isn't intended to be submitted anywhere. It's just there for the benefit of the Copy button:

<input type="button" value="Copy" onclick="copy(document.groovyform.simba.value)" />

There are also a number of references to document.groovyform in the buildCall Javascript function, which is run when you click on Build your widget.

This is an old style of Javascript programming. These days, most would assign IDs to these elements, and use document.getElementById() to access them, so there would be no need to wrap them in a form. But before that approach was developed, the way to access DOM elements depended on the fact that forms are automatically added as properties of document, and input elements are properties of the containing form.

Upvotes: 2

Reading Comprehension, I could learn it.

Upvotes: 1

Related Questions