Python - 请求/RoboBrowser - ASPX POST JavaScript [英] Python - Requests/RoboBrowser - ASPX POST JavaScript

查看:15
本文介绍了Python - 请求/RoboBrowser - ASPX POST JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植一个 bash 脚本,该脚本使用 curl 并将代码中的有效负载发布到 URL 并工作.基本问题是,使用 robobrowser,我在使用页面表单发布帖子时遇到了麻烦.

浏览网站:

  • 登录/SubLogin.aspx
  • 成功登录重定向/OptionsSummary.aspx
  • 使用参数获取/FindMe.aspx
  • POST/FindMe.aspx 按钮电话列表"(页面应加载电话列表"表,其中包含工作"项)
  • 选择Work"项目会向/PhoneLists.aspx 执行 POST(这应该会加载一个包含用户列表的Work"表.

我已经能够成功通过该站点的身份验证并使用 RoboBrowser 和 Requests+bs4 执行 GET我对自己发回页面感到困惑.

使用 RoboBrowser (liboncall.py)

#!/usr/bin/python从 robobrowser 导入 RoboBrowser从 bs4 导入 BeautifulSoup 作为 BSoc_mailbox = '123456'oc_password_hashed = 'ABCDEFG'base_uri = 'http://example.com'auth_uri = oc_base_uri + '/SubLogin.aspx'find_uri = oc_base_uri + '/FindMe.aspx'phne_uri = oc_base_uri + '/PhoneLists.aspx'p_auth_payload = {'SubLoginControl:javascriptTest': 'true','SubLoginControl:mailbox': 邮箱,'SubLoginControl:phoneNumber': '','SubLoginControl:password': password_hashed,'SubLoginControl:btnLogOn': '登录','SubLoginControl:webLanguage': 'en-US','SubLoginControl:initialLanguage': 'en-US','SubLoginControl:errorCallBackNumber': '输入的电话号码包含不可拨的字符.','SubLoginControl:cookieMailbox': '邮箱','SubLoginControl:cookieCallbackNumber': 'callbackNumber','子登录控件:服务器域':''}p_find_payload = {'FindMeControl:enableFindMe': 'on','FindMeControl:MasterDataControl:focusElement': '','FindMeControl:MasterDataControl:masterList:_ctl0:enabled': 'on','FindMeControl:MasterDataControl:masterList:_ctl0:itemGuid': '','FindMeControl:MasterDataControl:hidSelectedScheduleName': '','FindMeControl:MasterDataControl:hidbtnStatus': '','FindMeControl:MasterDataControl:hidScheduleXML': '','FindMeControl:MasterDataControl:tempScheduleXML': '','FindMeControl:MasterDataControl:hidSelectedScheduleGUID': '','FindMeControl:MasterDataControl:hidChangedScheduleList': '','FindMeControl:btnPhoneLists': '电话列表','FindMeControl:enableFindMeHidden': '','FindMeControl:applySet': 'false'}p_phne_payload = {'__EVENTARGUMENT': '','__EVENTTARGET': 'PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton','PhoneListsControl:MasterDataControl:focusElement': '','PhoneListsControl:MasterDataControl:masterList:_ctl0:itemGuid': '','PhoneListsControl:MasterDataControl:hidSelectedScheduleName': '','PhoneListsControl:MasterDataControl:hidbtnStatus': '','PhoneListsControl:MasterDataControl:hidScheduleXML': '','PhoneListsControl:MasterDataControl:tempScheduleXML': '','PhoneListsControl:MasterDataControl:hidSelectedScheduleGUID': '','PhoneListsControl:MasterDataControl:hidChangedScheduleList': '','PhoneListsControl:applySet': 'false'}def auth(邮箱,密码):浏览器 = RoboBrowser(history=False)browser.open(oc_auth_uri)登录 = browser.get_form(id='aspnetForm')signin['SubLoginControl:mailbox'].value = 邮箱signin['SubLoginControl:password'].value = 密码signin['SubLoginControl:javascriptTest'].value = 'true'登录['SubLoginControl:btnLogOn'].value = '登录'signin['SubLoginControl:webLanguage'].value = 'en-US'signin['SubLoginControl:initialLanguage'].value = 'en-US'signin['SubLoginControl:errorCallBackNumber'].value = '输入+电话+号码+包含+不可拨号+字符.'登录['SubLoginControl:cookieMailbox'].value = '邮箱'signin['SubLoginControl:cookieCallbackNumber'].value = 'callbackNumber'登录['SubLoginControl:serverDomain'].value = ''browser.submit_form(登录)返回浏览器

登录网站并显示 URL 以验证我们在:

在 [20]: from liboncall import *在 [21]: m = auth(oc_mailbox, oc_password_hashed)在 [22]: m.url出[22]:你'http://example.com/OptionsSummary.aspx'

打开/FindMe.aspx":

在 [24]: m.open(find_uri)在 [25]: m.url出[25]:你'http://example.com/FindMe.aspx'

最初/FindMe.aspx"将加载一个表单和一个电话列表"按钮,(FindMeControl:btnPhoneLists).

在 [26]: m.select('title')出[26]:[找到我]在 [27]: form_find_a = m.get_form(action="FindMe.aspx")在 [28] 中:对于 form_find_a.keys() 中的 i:打印(一)....:__视图状态__事件验证FindMeControl:启用FindMeFindMeControl:MasterDataControl:focusElementFindMeControl:MasterDataControl:masterList:_ctl0:enabledFindMeControl:MasterDataControl:masterList:_ctl0:itemGuidFindMeControl:MasterDataControl:btnAddFindMeControl:MasterDataControl:btnDeleteFindMeControl:MasterDataControl:btnRenameFindMeControl:MasterDataControl:btnCancelFindMeControl:MasterDataControl:btnEnterFindMeControl:MasterDataControl:btnUpdateFindMeControl:MasterDataControl:hidSelectedScheduleNameFindMeControl:MasterDataControl:hidbtnStatusFindMeControl:MasterDataControl:hidScheduleXMLFindMeControl:MasterDataControl:tempScheduleXMLFindMeControl:MasterDataControl:hidSelectedScheduleGUIDFindMeControl:MasterDataControl:hidChangedScheduleListFindMeControl:btnApplyFindMeControl:btnSchedulesFindMeControl:btnPhoneListsFindMeControl:enableFindMeHiddenFindMeControl:applySet

删除不需要的表单字段,填写表单并提交:

在 [29]: find_remove = ('FindMeControl:MasterDataControl:btnAdd','FindMeControl:MasterDataControl:btnDelete','FindMeControl:MasterDataControl:btnRename','FindMeControl:MasterDataControl:btnCancel','FindMeControl:MasterDataControl:btnEnter','FindMeControl:MasterDataControl:btnUpdate','FindMeControl:btnApply','FindMeControl:btnSchedules')在 [30] 中:对于 find_remove 中的 i:form_find_a.fields.pop(i)在 [31]: form_find_a['FindMeControl:enableFindMe'].value = 'on'form_find_a['FindMeControl:MasterDataControl:focusElement'].value = ''form_find_a['FindMeControl:MasterDataControl:masterList:_ctl0:enabled'].value = 'on'form_find_a['FindMeControl:MasterDataControl:masterList:_ctl0:itemGuid'].value = ''form_find_a['FindMeControl:MasterDataControl:hidSelectedScheduleName'].value = ''form_find_a['FindMeControl:MasterDataControl:hidbtnStatus'].value = ''form_find_a['FindMeControl:MasterDataControl:hidScheduleXML'].value = ''form_find_a['FindMeControl:MasterDataControl:tempScheduleXML'].value = ''form_find_a['FindMeControl:MasterDataControl:hidSelectedScheduleGUID'].value = ''form_find_a['FindMeControl:MasterDataControl:hidChangedScheduleList'].value = ''form_find_a['FindMeControl:btnPhoneLists'].value = '电话列表'form_find_a['FindMeControl:enableFindMeHidden'].value = ''form_find_a['FindMeControl:applySet'].value = 'false'出[31]:...在 [32]: m.submit_form(form_find_a)

验证该页面已更新并具有列表项工作":

在 [33]: m.parsed.find('title')Out[33]:<title>电话列表</title>在 [34]: m.parsed.find('a', id='PhoneListsControl_MasterDataControl_masterList__ctl0_SelectButton')Out[34]: <a class="linkBut​​tonItem" href="javascript:__doPostBack('PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton','')" id="PhoneListsControl_MasterDataControl_masterList__ctl0_SelectButton" onclick="javascript:onClick();">工作</a>

获取PhoneLists.aspx"表单,删除不需要的字段,填写并提交.

在[35]中:form_find_b = m.get_form(action='PhoneLists.aspx')在 [36] 中:phne_remove = ('PhoneListsControl:MasterDataControl:btnAdd','PhoneListsControl:MasterDataControl:btnDelete','PhoneListsControl:MasterDataControl:btnRename','PhoneListsControl:MasterDataControl:btnCancel','PhoneListsControl:MasterDataControl:btnEnter','PhoneListsControl:MasterDataControl:btnUpdate','PhoneListsControl:btnApply','PhoneListsControl:btnBack')在 [37] 中:对于 phne_remove 中的 i:form_find_b.fields.pop(i)在 [38]: form_find_b['PhoneListsControl:MasterDataControl:focusElement'].value = ''form_find_b['PhoneListsControl:MasterDataControl:hidChangedScheduleList'].value = ''form_find_b['PhoneListsControl:MasterDataControl:hidScheduleXML'].value = ''form_find_b['PhoneListsControl:MasterDataControl:hidSelectedScheduleGUID'].value = ''form_find_b['PhoneListsControl:MasterDataControl:hidSelectedScheduleName'].value = ''form_find_b['PhoneListsControl:MasterDataControl:hidbtnStatus'].value = ''form_find_b['PhoneListsControl:MasterDataControl:masterList:_ctl0:itemGuid'].value = ''form_find_b['PhoneListsControl:MasterDataControl:tempScheduleXML'].value = ''form_find_b['PhoneListsControl:applySet'].value = 'false'在 [39]: m.submit_form(form_find_b)

查看帖子以查看用户列表是否已加载.在这种情况下,它没有加载用户列表.

In [40]: m.parsed.findAll('div', id='PhoneListsControl_phoneListMembersText')输出[41]:[<div class="displayText" id="PhoneListsControl_phoneListMembersText"></div>]

如果成功,上面会返回:

<div id="PhoneListsControl_phoneListMembersText" class="displayText" style="top: 315px; left: 281px;">&nbsp;&nbsp;工作&nbsp;<;/div>

连同表中的以下项目,(PhoneListsControl_phoneListDetail):

<input name="PhoneListsControl:phoneListDetail:_ctl2:number" type="text" value="95551234567" maxlength="50" id="PhoneListsControl_phoneListDetail__ctl2_number" onkeyup="enableApplyButton('PhoneListsControl_')"样式=宽度:140像素;">...<input name="PhoneListsControl:phoneListDetail:_ctl3:number" type="text" value="95551236789" maxlength="50" id="PhoneListsControl_phoneListDetail__ctl2_number" onkeyup="enableApplyButton('PhoneListsControl_')" style="width:140px;">...

在这次冒险中,我发现 Robobrowser 没有包含所有必需的用于发布到PhoneLists.aspx"的表单数据按预期工作,('__EVENTTARGET':'PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton'__EVENTARGUMENT).设置参数然后执行 submit_form(form_find_b) 也没有达到预期的结果.我想知道 robobrowser.forms.form 中的 add_field() 是否可以工作,但我不明白如何正确使用它(如果要使用它的话)如我所愿.例如,将 __EVENTTARGET__EVENTARGUMENT 隐藏输入字段添加到表单中).

还有什么我遗漏的吗?或者 RoboBrowser/Requests 不支持这种类型的帖子?是否该表单需要 javascript 来执行 此处用机械化?

解决方案

已解决

经过多次谷歌搜索后,在 reddit 上重新发布寻求帮助然后随机绊倒 this RoboBrowser 问题,向我展示了如何正确使用fields.add_field"()' 方法;问题解决了.

例如

b_e_arg = robobrowser.forms.fields.Input('<input name="__EVENTARGUMENT" value="" />')b_e_target = robobrowser.forms.fields.Input('<input name="__EVENTTARGET" value="PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton" />')在 [30] 中:form_find_b.add_field(b_e_target)在 [31] 中:form_find_b.add_field(b_e_arg)

使用这些值更新表单后,提交到PhoneLists.aspx"的表单将按预期工作.

在[33]中:m.submit_form(form_find_b)在 [34]: m.url出 [34]: u'http://example/PhoneLists.aspx'在 [35]: m.parsed.findAll('div', id='PhoneListsControl_phoneListMembersText')输出[35]:[<div class="displayText" id="PhoneListsControl_phoneListMembersText">工作

]在 [36]: m.parsed.findAll('input', id='PhoneListsControl_phoneListDetail__ctl2_number')Out[36]: [<input id="PhoneListsControl_phoneListDetail__ctl2_number" maxlength="50" name="PhoneListsControl:phoneListDetail:_ctl2:number" onkeyup="enableApplyButton('PhoneListsControl_')" type="text" value="95551234567"/>]

我希望其他需要抓取 ASPX 站点的人会发现这很有用.祝大家黑客愉快!

I am porting a bash script that uses curl and POSTs the payloads in the code to the URL's and works. The basic issue is that, with robobrowser, I'm running into trouble posting using the page forms.

Stepping through the site:

  • Login to /SubLogin.aspx
  • Successful login redirects /OptionsSummary.aspx
  • GET /FindMe.aspx with params
  • POST /FindMe.aspx button "Phone Lists" (page should then load "Phone Lists" table with a item "Work")
  • Selecting the "Work" item executes a POST to /PhoneLists.aspx (this should then load a table "Work" with list of users.

I have been able to successfully authenticate to the site and perform GETs with both RoboBrowser and Requests+bs4 however I'm confused on POSTing back to the pages themselves.

Using RoboBrowser (liboncall.py)

#!/usr/bin/python

from robobrowser import RoboBrowser
from bs4 import BeautifulSoup as BS

oc_mailbox = '123456'
oc_password_hashed = 'ABCDEFG'

base_uri = 'http://example.com'
auth_uri = oc_base_uri + '/SubLogin.aspx'
find_uri = oc_base_uri + '/FindMe.aspx'
phne_uri = oc_base_uri + '/PhoneLists.aspx'


p_auth_payload = {
        'SubLoginControl:javascriptTest': 'true',
        'SubLoginControl:mailbox': mailbox,
        'SubLoginControl:phoneNumber': '',
        'SubLoginControl:password': password_hashed,
        'SubLoginControl:btnLogOn': 'Logon',
        'SubLoginControl:webLanguage': 'en-US',
        'SubLoginControl:initialLanguage': 'en-US',
        'SubLoginControl:errorCallBackNumber': 'Entered telephone number contains non-dialable characters.',
        'SubLoginControl:cookieMailbox': 'mailbox',
        'SubLoginControl:cookieCallbackNumber': 'callbackNumber',
        'SubLoginControl:serverDomain': ''
        }

p_find_payload = {
        'FindMeControl:enableFindMe': 'on',
        'FindMeControl:MasterDataControl:focusElement': '',
        'FindMeControl:MasterDataControl:masterList:_ctl0:enabled': 'on',
        'FindMeControl:MasterDataControl:masterList:_ctl0:itemGuid': '',
        'FindMeControl:MasterDataControl:hidSelectedScheduleName': '',
        'FindMeControl:MasterDataControl:hidbtnStatus': '',
        'FindMeControl:MasterDataControl:hidScheduleXML': '',
        'FindMeControl:MasterDataControl:tempScheduleXML': '',
        'FindMeControl:MasterDataControl:hidSelectedScheduleGUID': '',
        'FindMeControl:MasterDataControl:hidChangedScheduleList': '',
        'FindMeControl:btnPhoneLists': 'Phone Lists',
        'FindMeControl:enableFindMeHidden': '',
        'FindMeControl:applySet': 'false'
        }

p_phne_payload = {
        '__EVENTARGUMENT': '',
        '__EVENTTARGET': 'PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton',
        'PhoneListsControl:MasterDataControl:focusElement': '',
        'PhoneListsControl:MasterDataControl:masterList:_ctl0:itemGuid': '',
        'PhoneListsControl:MasterDataControl:hidSelectedScheduleName': '',
        'PhoneListsControl:MasterDataControl:hidbtnStatus': '',
        'PhoneListsControl:MasterDataControl:hidScheduleXML': '',
        'PhoneListsControl:MasterDataControl:tempScheduleXML': '',
        'PhoneListsControl:MasterDataControl:hidSelectedScheduleGUID': '',
        'PhoneListsControl:MasterDataControl:hidChangedScheduleList': '',
        'PhoneListsControl:applySet': 'false'
        }


def auth(mailbox, password):
    browser = RoboBrowser(history=False)
    browser.open(oc_auth_uri)

    signin = browser.get_form(id='aspnetForm')
    signin['SubLoginControl:mailbox'].value = mailbox
    signin['SubLoginControl:password'].value = password
    signin['SubLoginControl:javascriptTest'].value = 'true'
    signin['SubLoginControl:btnLogOn'].value = 'Logon'
    signin['SubLoginControl:webLanguage'].value = 'en-US'
    signin['SubLoginControl:initialLanguage'].value = 'en-US'
    signin['SubLoginControl:errorCallBackNumber'].value = 'Entered+telephone+number+contains+non-dialable+characters.'
    signin['SubLoginControl:cookieMailbox'].value = 'mailbox'
    signin['SubLoginControl:cookieCallbackNumber'].value = 'callbackNumber'
    signin['SubLoginControl:serverDomain'].value = ''

    browser.submit_form(signin)
    return browser

Login to site and show URL to verify we're in:

In [20]: from liboncall import *
In [21]: m = auth(oc_mailbox, oc_password_hashed)

In [22]: m.url
Out[22]: u'http://example.com/OptionsSummary.aspx'

Open "/FindMe.aspx":

In [24]: m.open(find_uri)

In [25]: m.url
Out[25]: u'http://example.com/FindMe.aspx'

Initially "/FindMe.aspx" will load a form and a button "Phone Lists", (FindMeControl:btnPhoneLists).

In [26]: m.select('title')
Out[26]: [<title>Find Me</title>]

In [27]: form_find_a = m.get_form(action="FindMe.aspx")

In [28]: for i in form_find_a.keys():
    print(i)
    ....:
    __VIEWSTATE
    __EVENTVALIDATION
    FindMeControl:enableFindMe
    FindMeControl:MasterDataControl:focusElement
    FindMeControl:MasterDataControl:masterList:_ctl0:enabled
    FindMeControl:MasterDataControl:masterList:_ctl0:itemGuid
    FindMeControl:MasterDataControl:btnAdd
    FindMeControl:MasterDataControl:btnDelete
    FindMeControl:MasterDataControl:btnRename
    FindMeControl:MasterDataControl:btnCancel
    FindMeControl:MasterDataControl:btnEnter
    FindMeControl:MasterDataControl:btnUpdate
    FindMeControl:MasterDataControl:hidSelectedScheduleName
    FindMeControl:MasterDataControl:hidbtnStatus
    FindMeControl:MasterDataControl:hidScheduleXML
    FindMeControl:MasterDataControl:tempScheduleXML
    FindMeControl:MasterDataControl:hidSelectedScheduleGUID
    FindMeControl:MasterDataControl:hidChangedScheduleList
    FindMeControl:btnApply
    FindMeControl:btnSchedules
    FindMeControl:btnPhoneLists
    FindMeControl:enableFindMeHidden
    FindMeControl:applySet

Remove un-needed form fields, fill out form and submit:

In [29]: find_remove = (
'FindMeControl:MasterDataControl:btnAdd',
'FindMeControl:MasterDataControl:btnDelete',
'FindMeControl:MasterDataControl:btnRename',
'FindMeControl:MasterDataControl:btnCancel',
'FindMeControl:MasterDataControl:btnEnter',
'FindMeControl:MasterDataControl:btnUpdate',
'FindMeControl:btnApply',
'FindMeControl:btnSchedules')

In [30]: for i in find_remove:
        form_find_a.fields.pop(i)

In [31]: form_find_a['FindMeControl:enableFindMe'].value = 'on'
form_find_a['FindMeControl:MasterDataControl:focusElement'].value = ''
form_find_a['FindMeControl:MasterDataControl:masterList:_ctl0:enabled'].value = 'on'
form_find_a['FindMeControl:MasterDataControl:masterList:_ctl0:itemGuid'].value = ''
form_find_a['FindMeControl:MasterDataControl:hidSelectedScheduleName'].value = ''
form_find_a['FindMeControl:MasterDataControl:hidbtnStatus'].value = ''
form_find_a['FindMeControl:MasterDataControl:hidScheduleXML'].value = ''
form_find_a['FindMeControl:MasterDataControl:tempScheduleXML'].value = ''
form_find_a['FindMeControl:MasterDataControl:hidSelectedScheduleGUID'].value = ''
form_find_a['FindMeControl:MasterDataControl:hidChangedScheduleList'].value = ''
form_find_a['FindMeControl:btnPhoneLists'].value = 'Phone Lists'
form_find_a['FindMeControl:enableFindMeHidden'].value = ''
form_find_a['FindMeControl:applySet'].value = 'false'
Out [31]: ...

In [32]: m.submit_form(form_find_a)

Verifying that page has updated and has the list item "Work":

In [33]: m.parsed.find('title')
Out[33]: <title>Phone Lists</title>

In [34]: m.parsed.find('a', id='PhoneListsControl_MasterDataControl_masterList__ctl0_SelectButton')
Out[34]: <a class="linkButtonItem" href="javascript:__doPostBack('PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton','')" id="PhoneListsControl_MasterDataControl_masterList__ctl0_SelectButton" onclick="javascript:onClick();">Work</a>

Get the "PhoneLists.aspx" form, remove un-needed fields, fill out and submit.

In [35]: form_find_b = m.get_form(action='PhoneLists.aspx')

In [36]: phne_remove = (
    'PhoneListsControl:MasterDataControl:btnAdd',
    'PhoneListsControl:MasterDataControl:btnDelete',
    'PhoneListsControl:MasterDataControl:btnRename',
    'PhoneListsControl:MasterDataControl:btnCancel',
    'PhoneListsControl:MasterDataControl:btnEnter',
    'PhoneListsControl:MasterDataControl:btnUpdate',
    'PhoneListsControl:btnApply',
    'PhoneListsControl:btnBack')

In [37]: for i in phne_remove:
            form_find_b.fields.pop(i)

In [38]: form_find_b['PhoneListsControl:MasterDataControl:focusElement'].value = ''             
form_find_b['PhoneListsControl:MasterDataControl:hidChangedScheduleList'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:hidScheduleXML'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:hidSelectedScheduleGUID'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:hidSelectedScheduleName'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:hidbtnStatus'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:masterList:_ctl0:itemGuid'].value = ''
form_find_b['PhoneListsControl:MasterDataControl:tempScheduleXML'].value = ''
form_find_b['PhoneListsControl:applySet'].value = 'false'

In [39]: m.submit_form(form_find_b)

Review the post to see if user list loaded. In this instance, it did not load the user list.

In [40]: m.parsed.findAll('div', id='PhoneListsControl_phoneListMembersText')
Out[41]: [<div class="displayText" id="PhoneListsControl_phoneListMembersText"></div>]

If it was successfull the above would return:

<div id="PhoneListsControl_phoneListMembersText" class="displayText" style="top: 315px; left: 281px;">&nbsp;&nbsp;Work&nbsp;&nbsp;</div>

Along with the following items in a table, (PhoneListsControl_phoneListDetail):

<input name="PhoneListsControl:phoneListDetail:_ctl2:number" type="text" value="95551234567" maxlength="50" id="PhoneListsControl_phoneListDetail__ctl2_number" onkeyup="enableApplyButton('PhoneListsControl_')" style="width:140px;">
...
<input name="PhoneListsControl:phoneListDetail:_ctl3:number" type="text" value="95551236789" maxlength="50" id="PhoneListsControl_phoneListDetail__ctl2_number" onkeyup="enableApplyButton('PhoneListsControl_')" style="width:140px;">
...

At this venture I figured out that Robobrowser isn't including all the required formdata for the post to "PhoneLists.aspx" to work as expected, ('__EVENTTARGET':'PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton' and __EVENTARGUMENT). Setting the params and then doing submit_form(form_find_b) does not achieve desired results either. I wonder if the add_field() from robobrowser.forms.form would work but I'm not understanding how to properly utilize it, (if it is to be used at all as I wanted. e.g. Add the __EVENTTARGET and __EVENTARGUMENT hidden input fields to the form).

Is there something else I am missing or does RoboBrowser/Requests not support this type of post? Is it that the form requires javascript to execute as mentioned here with mechanize?

解决方案

Solved

After much googling, re-posting for help on reddit and then randomly stumbling this RoboBrowser issue that showed me how to properly use the 'fields.add_field()' method; the problem is solved.

e.g.

b_e_arg = robobrowser.forms.fields.Input('<input name="__EVENTARGUMENT" value="" />')

b_e_target = robobrowser.forms.fields.Input('<input name="__EVENTTARGET" value="PhoneListsControl$MasterDataControl$masterList$_ctl0$SelectButton" />')

In [30]: form_find_b.add_field(b_e_target)
In [31]: form_find_b.add_field(b_e_arg)

Once the form was updated with these values, the form submit to "PhoneLists.aspx" works as expected.

In [33]: m.submit_form(form_find_b)

In [34]: m.url
Out[34]: u'http://example/PhoneLists.aspx'

In [35]: m.parsed.findAll('div', id='PhoneListsControl_phoneListMembersText')
Out[35]: [<div class="displayText" id="PhoneListsControl_phoneListMembersText">  Work  </div>]

In [36]: m.parsed.findAll('input', id='PhoneListsControl_phoneListDetail__ctl2_number')
Out[36]: [<input id="PhoneListsControl_phoneListDetail__ctl2_number" maxlength="50" name="PhoneListsControl:phoneListDetail:_ctl2:number" onkeyup="enableApplyButton('PhoneListsControl_')" type="text" value="95551234567"/>]

I hope anyone else that has to scrape ASPX sites finds this useful. Happy hacking to all!

这篇关于Python - 请求/RoboBrowser - ASPX POST JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆