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

问题描述:

我试图使用python请求模块发出Post请求,但我最感兴趣的输入只有一个id和没有name属性。我见过的所有例子都涉及到使用该名称属性。我怎样才能做到这一点Post请求以下表格:使用Python请求模块提交没有输入名称的表单

    <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) 
+0

什么网址是什么? –

+0

JavaScript函数'keepSearch'做什么?它是否修改页面源代码? –

+0

你可以安装拦截表单提交的浏览器插件并显示提交的值,然后手动尝试表单来查看实际发送的内容吗? –

如果你把在Firebug或Chrome开发者工具来看看你可以看到的后提出请求:

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"}) 

哪,如果你运行它,你会看到网址,以获取构建正确:

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