无法使用Python登录网页

时间:2014-04-04 02:52:30

标签: python request web-scraping session-cookies

我已经尝试了很多方法在Python中登录网页但是从返回的值看起来似乎无法正常工作。有人可以帮忙吗?以下是我的试验:

from requests import session

payload = {
    'action': 'login',
    'companyName': mycompanyname,
    'username': myusername,
    'password': mypassword }

with session() as c:
    c.post(url_main, data=payload)
    request = c.get(url_hidden)
    print request.headers
    print request.text

这会在我实际登录之前返回消息。

import urllib2, cookielib
login_data = urllib.urlencode({'companyName':mycompanyname,'username' : myusername, 'password' : mypassword})
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.open(url_main, login_data)
myfile = opener.open(url_hidden)

output = open('t1.txt', 'w')
output.write(myfile.read())
output.close()

同样,t1.txt中提到的内容也是我登录前的信息。

我已经检查了网页的源代码,它就像是:

<DIV class=labelFix><LABEL for=companyName><STRONG>Company</STRONG>:</LABEL></DIV>
<DIV class=fieldFix><INPUT tabIndex=1 id=companyName size=8 name=companyName dpieagent_iecontroltype="2"> </DIV><BR>
<DIV class=labelFix><LABEL for=username><STRONG>Username</STRONG>:</LABEL></DIV>
<DIV class=fieldFix><INPUT tabIndex=2 id=username size=8 name=username dpieagent_iecontroltype="2"> </DIV><BR>
<DIV class=labelFix><LABEL for=password><STRONG>Password</STRONG>:</LABEL></DIV>
<DIV class=fieldFix><INPUT tabIndex=3 id=password maxLength=16 size=8 type=password name=password dpieagent_iecontroltype="3"> <INPUT id=status type=hidden name=status> </DIV><BR>
<DIV class=searchBtnFix><INPUT tabIndex=4 id=btnLogin class=searchBtn type=submit value=Log-on name=submit> <INPUT onclick="location.href='logout.asp?redir=/default.asp?action=article&ID=1'" tabIndex=5 id=btnLogout class=searchBtn type=button value=Log-out name=button> 
<P style="PADDING-TOP: 0px; PADDING-LEFT: 84px"><A href="/useofcookies"><FONT size=1>Use of cookies</FONT></A></P></DIV></FORM></TD>
<TD class=container1b vAlign=top>

因此登录信息确实应该是&#39; companyName&#39;,&#39; username&#39;和密码&#39;对?如果我在网站上登录,我必须点击一个按钮&#39;登录&#39; - 但我不能用Python做到这一点。

0 个答案:

没有答案