Using Python Requests Module to Submit a Form with

2019-06-07 18:48发布

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)

1条回答
Viruses.
2楼-- · 2019-06-07 19:30

If you take a look in firebug or chrome developer tools you can see how the post request is made:

enter image description here

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&registry=Q'>#Zenyatta-QH-5154943</a></td>
                        <td >2009</td>
                        <td >Gelding</td>
                        <td >
                            <a href='/profiles/Results.cfm?type=Horse&refno=7237823&registry=Q'>#Mr Ice Te-QH</a> 
                        </td>
                        <td >
                            <a href='/profiles/Results.cfm?type=Horse&refno=6342673&registry=Q'>#She Sings Soprano-QH</a> 
                        </td>
                    </tr>

                    <tr>
                        <td ><a href='/profiles/Results.cfm?type=Horse&refno=7156465&registry=T'>Zenyatta</a></td>
                        <td >2004</td>
                        <td >Mare</td>
                        <td >
                            <a href='/profiles/Results.cfm?type=Horse&refno=4531602&registry=T'>Street Cry (IRE)</a> 
                        </td>
                        <td >
                            <a href='/profiles/Results.cfm?type=Horse&refno=4004138&registry=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
查看更多
登录 后发表回答