Reputation: 161
I'm trying to make a Post request using the python request module, but the input I am most interested in only has an id and no name attribute. And all the examples I've seen involve using that name attribute. How can I do this Post request for the following form:
<form id="search" method="post">
<select id="searchOptions" onchange="javascript:keepSearch(this);">
<option value="horses" selected>Horses</option>
<option value="jockeys">Jockeys</option>
<option value="trainers">Trainers</option>
<option value="owners">Owners</option>
<option value="tracks">Tracks</option>
<option value="stakes">Gr. Stakes</option>
</select>
<input type="hidden" id="searchVal" value="horses" name="searchVal">
<input class="input" id="searchInput" type="text" placeholder="Horse Name">
<span class="glyphicon glyphicon-search"></span>
<input type="submit" value="">
<span style="clear: both;">.</span>
</form>
I'm looking specifically at the input with the id="searchInput".
Currently, I'm trying this code: (which is only getting me the original homepage with the search bar)
data = {
'searchInput': name,
'searchVal' : "horses"
}
r = requests.post(self.equibaseHomeUrl, data=data)
Upvotes: 2
Views: 14910
Reputation: 180481
If you take a look in firebug or chrome developer tools you can see how the post request is made:
So using that we can:
p = {"searchVal":"horses",
"horse_name":"zenyatta"}
import requests
r = requests.post("http://www.equibase.com/profiles/Results.cfm?type=Horse",p)
print(r.content)
Which if you look at the content you can see the search result for Zenyatta.
<table class="table-hover">
<tr>
<th>Horse</th>
<th>YOB</th>
<th>Sex</th>
<th>Sire</th>
<th>Dam</th>
</tr>
<tr>
<td ><a href='/profiles/Results.cfm?type=Horse&refno=8575618®istry=Q'>#Zenyatta-QH-5154943</a></td>
<td >2009</td>
<td >Gelding</td>
<td >
<a href='/profiles/Results.cfm?type=Horse&refno=7237823®istry=Q'>#Mr Ice Te-QH</a>
</td>
<td >
<a href='/profiles/Results.cfm?type=Horse&refno=6342673®istry=Q'>#She Sings Soprano-QH</a>
</td>
</tr>
<tr>
<td ><a href='/profiles/Results.cfm?type=Horse&refno=7156465®istry=T'>Zenyatta</a></td>
<td >2004</td>
<td >Mare</td>
<td >
<a href='/profiles/Results.cfm?type=Horse&refno=4531602®istry=T'>Street Cry (IRE)</a>
</td>
<td >
<a href='/profiles/Results.cfm?type=Horse&refno=4004138®istry=T'>Vertigineux</a>
</td>
</tr>
</table>
Or if you want to use the base url and pass the query:
data = {"searchVal": "horses",
"horse_name": "zenyatta"}
import requests
r = requests.post("http://www.equibase.com/profiles/Results.cfm",
data, params={"type": "Horse"})
Which if you run it you will see url get constructed correctly:
In [11]: r = requests.post("http://www.equibase.com/profiles/Results.cfm",
....: data, params={"type": "Horse"})
In [12]:
In [12]: print(r.url)
http://www.equibase.com/profiles/Results.cfm?type=Horse
Upvotes: 4