ASPX请求浏览器登录仿真

时间:2012-11-01 00:44:47

标签: python mechanize lxml python-requests scraperwiki

我正在尝试在aspx网页上发帖。 我已成功完成登录并尝试获取页面内容但没有运气。

登录页面后转到redirect tmp.aspx,然后会显示主页面。

我的代码当前登录并显示tmp.aspx。 我想要的是我的脚本显示真实的页面。

所以当前流程登录 - > tmp(显示) 我想要登录的流程 - > tmp->默认(显示)

import lxml.html
import requests
starturl = 'http://*/login.aspx'
s = requests.session() # create a session object
r1 = s.get(starturl) #get page 1
html = r1.text
root = lxml.html.fromstring(html)

#pick up the javascript values
EVENTVALIDATION = root.xpath('//input[@name="__EVENTVALIDATION"]')[0].attrib['value']
#find the __EVENTVALIDATION value
VIEWSTATE = root.xpath('//input[@name="__VIEWSTATE"]')[0].attrib['value']
#find the __VIEWSTATE value
# build a dictionary to post to the site with the values we have collected. The __EVENTARGUMENT can be changed to fetch another result page (3,4,5 etc.)
payload = {'__EVENTTARGET':'TransactBtn','__EVENTARGUMENT':'','__EVENTVALIDATION':EVENTVALIDATION,'__VIEWSTATE':VIEWSTATE,'AccountID':'557','UserName':'*','Password':'*'}

# post it
r2 = s.post(starturl, data=payload)
# our response is now page 2
print r2.text

请指教? 感谢

tmp.aspx源代码。 (这是我执行脚本时显示的内容)

<body>
    <form name="form1" method="post" action="tmp.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZCOgyU+AdP30f85W4DdUIV6LnCqa" />

    <script type="text/javascript">
        top.location.href = document.location.href;               
        document.forms["form1"].submit();        
    </script>
        &nbsp;&nbsp;</form>
</body>

0 个答案:

没有答案