【问题标题】:Using Python Requests Module to Submit a Form without Input Name使用 Python 请求模块提交没有输入名称的表单
【发布时间】:2016-06-22 21:35:19
【问题描述】:

我正在尝试使用 python 请求模块发出 Post 请求,但我最感兴趣的输入只有一个 id 而没有 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)

【问题讨论】:

  • JavaScript 函数keepSearch 有什么作用?是否修改页面源?
  • 您能否安装一个浏览器插件,拦截表单提交并向您显示正在提交的值,然后手动尝试表单以查看实际发送的内容?
  • 我还没有安装插件,你会推荐一个特定的插件吗? @padraic 坎宁安,equibase.com/homehorseplayer.cfm
  • 可能是实时 HTTP 标头或 Firebug?

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


【解决方案1】:

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

所以我们可以使用它:

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

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

如果您查看内容,您可以看到禅雅塔的搜索结果。

<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 2017-08-06
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多