【问题标题】:Python-requests module, post two "values" to renew&scrape websitePython-requests 模块,将两个“值”发布到更新和抓取网站
【发布时间】:2018-07-25 23:20:45
【问题描述】:

第一部分已经回答了,但是 EDIT 没有回答。

我正在使用 python 和 requests 模块来抓取网站。因此,我必须“单击”一个更新按钮,它是一个链接(href),包含在图像“pat_renewmark.gif”中。

html

<form name="checkout_form" method="POST" id="checkout_form">
    <input type="HIDDEN" id="checkoutpagecmd">
    <a href="#" onclick="return submitCheckout( 'sortByCheckoutDate', 'bycheckoutdate' )"> 
        <img src="/screens/pat_sortbychkout.gif" alt="SORT BY DATE CHECKED OUT" border="0">
    </a>
    <input type="HIDDEN" name="currentsortorder" value="current_duedate">
    <a href="#" onclick="return submitCheckout( 'requestRenewSome', 'requestRenewSome' )">
        <img src="/screens/pat_renewmark.gif" alt="RENEW SELECTED ITEMS" border="0">
    </a>
</form>

javascript (submitCheckout)

function submitCheckout(buttonname, buttonvalue)
{
    var oHiddenID;
    oHiddenID = document.getElementById("checkoutpagecmd");

    oHiddenID.name = buttonname;
    oHiddenID.value = buttonvalue;

    //c29364j/c1365070 - prevent the patron from submitting twice
    var oButtonSpan;
    oButtonSpan = document.getElementById("checkoutbuttons0");
    if (oButtonSpan) oButtonSpan.style.display = "none";
    oButtonSpan = document.getElementById("checkoutbuttons1");
    if (oButtonSpan) oButtonSpan.style.display = "none";

    document.getElementById("checkout_form").submit();
    return true;
}

显然submitCheckout 通过了.namevalue,它们都分配给了”requestRenewSome”’, to the hidden input with theid=“checkoutpagecmd”`。


我之前使用过 requests 模块,我能够处理简单的用户名和密码输入,例如:

html

<div class="formEntryArea">
    <label for="extpatid">
         <span class="formLabel">
        Your username:
        </span>
    </label>
    <input name="extpatid" id="extpatid" value="" size="20" maxlength="40">
    <label for="extpatpw">
        <span class="formLabel">
        Your password:
        </span>
    </label>
    <input name="extpatpw" id="extpatpw" type="PASSWORD" value="" size="20" maxlength="40">
</div>

蟒蛇

import requests

with requests.Session() as c:

        LOGIN_URL = "https://example.com"
        USERNAME = “XXXXX”
        PASSWORD = “YYYYY”

        source = c.get(LOGIN_URL)

        data_load = dict(extpatid=USERNAME,extpatpw=PASSWORD)
        head_load = dict(referer=LOGIN_URL)

        c.post(LOGIN_URL, data=data_load, headers=head_load)

但是,这里 c.post 只处理每个输入的一个“值”(用户名或密码),并且不包含任何 javascript 代码。
看来,对于上述问题,我必须以某种方式发布两个属性/字符串
.name = 'requestRenewSome'
.value = 'requestRenewSome' ?还是方法与我附上的示例完全不同?


编辑

matino 的回答(或 t.m.adam 的评论)解决了问题!不幸的是,用户必须通过单击“是”按钮来确认他确定要续订。

html

<form name="checkout_form" method="POST" id="checkout_form">
    <input type="HIDDEN" id="checkoutpagecmd">
    <input type="HIDDEN" name="currentsortorder" value="current_duedate">
    <span id="checkoutbuttons0">
        <input type="SUBMIT" name="renewsome" value="YES">
        <input type="SUBMIT" name="donothing" value="NO">
    </span>
</form>

因此,我将'renewsome': 'YES' 添加到data_load 字典中,但这还不够。我不知道隐藏输入的值? id=checkoutpagecmd 和/或? name=currentsortorder 但找不到任何关于如何继续的答案。

附:我知道这实际上是一个已知问题,如果有人回答,我将把它分开。

【问题讨论】:

  • 那么data = {'requestRenewSome':'requestRenewSome'}?
  • 谢谢@t.m.adam ,解决了!不幸的是,发生了另一个问题,我在 EDIT 部分添加了该问题。
  • 如果没有实际的 url,很难判断 requests 是否可行。为什么不检查网络流量以查看单击按钮时提交了哪些数据?
  • 感谢您的帮助,但是答案很简单哈哈,一个空字符串“”

标签: javascript python http python-requests


【解决方案1】:

javascript 代码实际所做的是将名称和值动态分配给隐藏的输入。所以最终可能有两种情况:

<input type="hidden" id="checkoutpagecmd" name="sortByCheckoutDate" value= "bycheckoutdate">

<input type="hidden" id="checkoutpagecmd" name="requestRenewSome" value= "requestRenewSome">

知道了,就可以这样发送http请求了:

requests.post(url, data={'sortByCheckoutDate': 'bycheckoutdate'})  # 1st case
requests.post(url, data={'requestRenewSome': 'requestRenewSome'})  # 2nd case

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多