如何以Python编程方式登录网站 [英] How to programmatically log into website in Python

查看:55
本文介绍了如何以Python编程方式登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个Internet上进行了搜索,查看了许多示例,并尝试了所有发现的示例,但没有一个对我有用,所以请不要认为这是重复的-我需要我的帮助具体情况.

I have searched all over the Internet, looking at many examples and have tried every one I've found, yet none of them are working for me, so please don't think this is a duplicate - I need help with my specific case.

我正在尝试使用Python登录网站(在这种情况下,我正在尝试使用v2.7,但我并不反对使用更新的版本,只是我已经找到了最多的信息2.7).

I'm trying to log into a website using Python (in this instance I'm trying with v2.7 but am not opposed to using a more recent version, it's just I've been able to find the most info on 2.7).

我需要填写一个简短的表格,仅包含用户名和密码.我需要填写并登录的网页形式如下(我知道这很乱):

I need to fill out a short form, consisting simply of a username and password. The form of the webpage I need to fill out and log in to is as follows (it's messy, I know):

<form method="post" action="login.aspx?ReturnUrl=..%2fwebclient%2fstorepages%2fviewshifts.aspx" id="Form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTU4MTgwOTM1NWRkBffWXYjjifsi875vSMg9OVkhxOQYYstGTNcN9/PFb+M=" />
</div>

<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAVrmuRkG3j6RStt7rezNSLKVK7BrRAtEiqu9nGFEI+jB3Y2+Mc6SrnAqio3oCKbxYY85pbWlDO2hADfoPXD/5td+Ot37oCEEXP3EjBFcbJhKJGott7i4PNQkjYd3HFozLgRvbhbY2j+lPBkCGQJXOEe" />
</div>
            <div><span></span>
                <table style="BORDER-COLLAPSE: collapse" borderColor="#000000" cellSpacing="0" cellPadding="0"
                    width="600" align="center" border="1">
                    <tr>
                        <td>
                            <table cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
                                <tr>
                                    <td width="76%"><span id="centercontentTitle"></span>
                                        <H1 align="center"><br>
                                            <span>
                                                <IMG height="52" src="../images/logo-GMR.jpg" width="260"></span><span><br>
                                            </span></H1>
                                        <div id="centercontentbody">
                                            <div align="center">
                                                <TABLE width="350">
                                                    <TR>
                                                        <TD class="style7">Username:</TD>
                                                        <TD>
                                                            <div align="right"><input name="txtUsername" type="text" id="txtUsername" style="width:250px;" /></div>
                                                        </TD>
                                                    </TR>
                                                    <TR>
                                                        <TD class="style7">Password:</TD>
                                                        <TD>
                                                            <div align="right"><input name="txtPassword" type="password" id="txtPassword" style="width:250px;" /></div>
                                                        </TD>
                                                    </TR>
                                                    <TR>
                                                        <TD></TD>
                                                        <TD align="right"><input type="submit" name="btnSubmit" value="Submit" id="btnSubmit" /><input type="submit" name="btnCancel" value="Cancel" id="btnCancel" /></TD>
                                                    </TR>
                                                    <TR>
                                                        <TD colspan="2" align="center"></TD>
                                                    </TR>
                                                </TABLE>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <div align="center" style='height:250px'></div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <br>
                <br>
                <p>&nbsp;</p>
        </form>

通过在线搜索,我发现填写此表格并登录网站的最佳Python代码如下:

From searching around online, the best Python code I have found to fill out this form and log into the website is as follows:

注意:这不是我的代码,我从问题/示例,很多人说他们发现它很好用.

Note: This is not my code, I got it from this question/example, where many people have said they've found it to work well.

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', 'LoginTesting')]

# 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 = '<URL I am trying to log into>'

# Input parameters we are going to send
payload = {
  '__EVENTVALIDATION': '/wEdAAVrmuRkG3j6RStt7rezNSLKVK7BrRAtEiqu9nGFEI+jB3Y2+Mc6SrnAqio3oCKbxYY85pbWlDO2hADfoPXD/5td+Ot37oCEEXP3EjBFcbJhKJGott7i4PNQkjYd3HFozLgRvbhbY2j+lPBkCGQJXOEe"',
  'txtUsername': '<USERNAME>',
  'txtPassword': '<PASSWORD>',
  }

# 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()

不幸的是,这对我不起作用,我无法弄清楚原因.如果有人可以,请查看代码,并告诉我如何改进它,以使其正常工作.非常感谢!

Unfortunately, this is not working for me and I'm unable to figure out why. If someone could please please please look over the code and tell me how I could improve it so as it works as it should. It would be so greatly appreciated!

在此先感谢您收到的所有帮助:)

Thanks in advance for all help I receive :)

推荐答案

__ EVENTVALIDATION可能不是静态的,您需要在python中加载登录页面,获取__EVENTVALIDATION字段,然后进行登录.

__EVENTVALIDATION is probably not static, you need to load the login page in python, get the __EVENTVALIDATION field and then do the login.

类似的事情应该起作用:

Something like this should work:

import requests
from bs4 import BeautifulSoup

s = requests.session()

def get_eventvalidation():
    r = s.get("http://url.to.login.page")
    bs = BeautifulSoup(r.text)

    return bs.find("input", {"name":"__EVENTVALIDATION"}).attrs['value']

authentication_url = '<URL I am trying to log into>'

payload = {
  '__EVENTVALIDATION': get_eventvalidation(),
  'txtUsername': '<USERNAME>',
  'txtPassword': '<PASSWORD>',
  }

login = s.post(authentication_url, data=payload)

print login.text

您需要请求模块和beautifulsoup4.或者,您也可以重写它以不使用库.

You need the requests module and beautifulsoup4. Or you can just rewrite it to not use libraries.

您可能需要__VIEWSTATE作为POST值.

You probably need __VIEWSTATE as a POST value.

这篇关于如何以Python编程方式登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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