python urllib表单帖子

时间:2014-10-13 08:10:42

标签: python urllib

<div id="login-section">
    <fieldset class="validation-group">
        <table id="navLgnMember" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
    <tr>
        <td>
                    <div id="login-user">
                        <div class="input" id="username-wrapper">
                            <div class="loginfield-label"><a href="/help" title="click for help logging in" style="color: White; text-decoration: none">Number / ID / Email</a></div>
                            <div class="input-field-small float-left submit-on-enter"><div class="left"></div><input name="ctl00$ctl01$navLgnMember$Username" type="text" maxlength="80" id="Username" title="Username" class="center" style="width:85px;" /><div class="right"></div></div>
                        </div>
                        <div class="input" id="password-wrapper">
                            <div class="loginfield-label">
                                Password</div>
                            <div class="input-field-small float-left submit-on-enter"><div class="left"></div><input name="ctl00$ctl01$navLgnMember$Password" type="password" id="Password" title="Password" class="center" title="Password" style="width:85px;" /><div class="right"></div></div>
                        </div>
                        <div id="login-wrapper">
                            <input type="submit" name="ctl00$ctl01$navLgnMember$Login" value="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl01$navLgnMember$Login&quot;, &quot;&quot;, false, &quot;&quot;, &quot;https://tatts.com/tattersalls&quot;, false, false))" id="Login" class="button-login" />

                        </div>

如何从urllib提交此表单作为我当前的代码:

import cookielib
import urllib
import urllib2


# Store the cookies and create an opener that will hold them
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# Add our headers
opener.addheaders = [('User-agent', 'RedditTesting')]

# Install our opener (note that this changes the global opener to the one
# we just made, but you can also just call opener.open() if you want)
urllib2.install_opener(opener)

# The action/ target from the form
authentication_url = 'https://tatts.com/tattersalls'

# Input parameters we are going to send
payload = {
  '_EVENTTARGET:' ''
  '__EVENTARGUMENT:' ''
  '__VIEWSTATE:' '/wEPDwUKMTIwNzM2NDc5NQ9kFgICCBBkZBYCAgcPZBYGZg9kFgJmD2QWAmYPFgIeB1Zpc2libGVoZAIBD2QWAmYPZBYCZg8WAh8AaGQCAg9kFgJmD2QWAgIBD2QWAmYPZBYCZg9kFgYCAw9kFgICBw8WAh4FY2xhc3MFFmxhdGVzdFJlc3VsdHNCb2R5RGl2QmdkAgsPZBYCZg9kFgICBQ8WBB4JaW5uZXJodG1sBR8qRGl2IDEgJDFNIGZvciB1cCB0byA0IHdpbm5lcnMuHwBnZAIND2QWAmYPZBYCZg9kFgYCAQ8PFgIeBFRleHQFNVdobyB3b24gbW9uZXkgaW4gRHJvbWFuYT8gVGF0dHNMb3R0byBwcml6ZSB1bmNsYWltZWQhZGQCAw8PFgIfAwV5QSBkaXZpc2lvbiBvbmUgVGF0dHNMb3R0byBwcml6ZSBvZiAkODI5LDM2MS42OCB3YXMgd29uIGluIERyb21hbmEgaW4gU2F0dXJkYXkgbmlnaHTigJlzIGRyYXcgYnV0IHRoZSB3aW5uZXIgaXMgYSBteXN0ZXJ5IWRkAgUPDxYCHgtOYXZpZ2F0ZVVybAUbL3RhdHRlcnNhbGxzL3dpbm5lci1zdG9yaWVzZGRk40y89P1oSwLqvsMH4ZGTu9vsloo='
  '__PREVIOUSPAGE:' 'PnGXOHeTQRfdct4aw9jgJ_Padml1ip-t05LAdAWQvBe5-2i1ECm5zC0umv9-PrWPJIXsvg9OvNT2PNp99srtKpWlE4J-6Qp1mICoT3eP49RSXSmN6p_XiieWS68YpbKqyBaJrkmYbJpZwCBw0Wq3tSD3JUc1'
  '__EVENTVALIDATION:': '/wEdAAfZmOrHFYG4x80t+WWbtymCH/lQNl+1rLkmSESnowgyHVo7o54PGpUOvQpde1IkKS5gFTlJ0qDsO6vsTob8l0lt1XHRKk5WhaA0Ow6IEfhsMPG0mcjlqyEi79A1gbm2y9z5Vxn3bdCWWa28kcUm81miXWvi1mwhfxiUpcDlmGDs/3LMo4Y='
  'ctl00$ctl01$showUpgradeReminderHid:' 'false'
  'ctl00$ctl01$navLgnMember$Username:' 'x-tinct'
  'ctl00$ctl01$navLgnMember$Password:' '########'
  'ctl00$ctl01$navLgnMember$Login:'

  }

# Use urllib to encode the payload
data = urllib.urlencode(payload)

# Build our Request object (supplying 'data' makes it a POST)
req = urllib2.Request(authentication_url, data)

# Make the request and read the response
resp = urllib2.urlopen(req)
contents = resp.read()

print (resp)

是一种公平的方式,可以提交到网络的正确部分。

我试图登录并创建一个会话,然后能够将更多的webform数据发布到网站的其他部分。

提前致谢。

1 个答案:

答案 0 :(得分:0)

根据SO的另一篇文章:Mechanize and Javascript,你有不同的选择,从在Python中模拟javascript脚本正在做什么,到使用完整的Selenium及其Python绑定。

如果您尝试继续使用简单的Python方式,我强烈建议您使用网络间谍(例如优秀的wireshark)来分析通过真实浏览器成功登录的实际内容和发送方式,以及您的内容Python模拟发送。

相关问题