2016-03-08 25 views
1

我試圖使用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

你可以安裝攔截表單提交的瀏覽器插件並顯示提交的值,然後手動嘗試表單來查看實際發送的內容嗎? –

回答

2

如果你把在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 
相關問題