使用Python请求模块提交不带输入Nam的表单

2024-09-27 04:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python请求模块发出Post请求,但我最感兴趣的输入只有id和no name属性。我看到的所有例子都涉及到使用name属性。我怎样才能申请以下表格:

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

我特别关注的是带有^{id1}的输入$

目前,我正在尝试这个代码:(这只是给我的原始主页与搜索栏)

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

Tags: nameformidinputsearchdata属性value
1条回答
网友
1楼 · 发布于 2024-09-27 04:20:18

如果您查看firebug或chrome开发工具,可以看到post请求是如何发出的:

enter image description here

因此我们可以:

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>

或者,如果要使用基本url并传递查询:

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

相关问题 更多 >

    热门问题