使用Python请求模块提交没有输入名称的表单

时间:2016-03-08 19:06:03

标签: python html http web-scraping python-requests

我正在尝试使用python请求模块发出Post请求,但我最感兴趣的输入只有id而没有name属性。我见过的所有示例都涉及使用该name属性。我该怎么做以下表格的Post请求:

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

我正在专门研究id =“searchInput”的输入。

目前,我正在尝试使用此代码:(这只是带有搜索栏的原始主页)

    data = {
        'searchInput': name,
        'searchVal' : "horses"
    }
    r = requests.post(self.equibaseHomeUrl, data=data)

1 个答案:

答案 0 :(得分:3)

如果您查看firebug或chrome开发人员工具,您可以看到发布请求的方式:

Using Cython Declarations from C

所以使用它我们可以:

p = {"searchVal":"horses",
"horse_name":"zenyatta"}

import requests
r = requests.post("http://www.equibase.com/profiles/Results.cfm?type=Horse",p)
print(r.content)

如果您查看内容,可以看到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>

或者,如果您想使用基本网址并传递查询:

data = {"searchVal": "horses",
     "horse_name": "zenyatta"}
import requests

r = requests.post("http://www.equibase.com/profiles/Results.cfm", 
                  data, params={"type": "Horse"})

如果你运行它,你会看到url被正确构造:

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