我想用我的用户名和密码登录网站,但我似乎无法得到它的工作。现在我正在使用该网站进行测试。如果我能够登录,我应该能够找到说SnowBomb白金会员资格的位。如果我无法登录,我应该在源代码中找到lost_password文本。用python登录网站
这是我有:
info = {
'USERNAME' : 'username',
'PASSWORD' : 'password',
#'submit' : 'login' don't know if i need this
}
def main():
r = requests.post('http://www.snowbomb.com/my-account-2', data = info) #logged in
request = requests.get('http://www.snowbomb.com/my-account-2')
if 'lost_password' in request.content:
print 'Was not able to log in'
print 'lost_password' in request.content
else:
if 'The SnowBomb Platinum Membership' in request.content:
print 'Logged in'
print 'The SnowBomb Platinum Membership' in request.content #--> when it works
if __name__ == '__main__':
main()
这里是要求用户登录时,在源代码的形式:
<form method="post" class="login">
<p class="form-row form-row-first">
<label for="username">Username <span class="required">*</span></label>
<input type="text" class="input-text" name="username" id="username" />
</p>
<p class="form-row form-row-last">
<label for="password">Password <span class="required">*</span></label>
<input class="input-text" type="password" name="password" id="password" />
</p>
<div class="clear"></div>
<p class="form-row">
<input type="hidden" id="_n" name="_n" value="a046c51363" /><input type="hidden" name="_wp_http_referer" value="/my-account-2/" /><input type="hidden" name="redirect" value="http://www.snowbomb.com/my-account"/>
<input type="submit" class="button" name="login" value="Login" />
<a class="lost_password" href="http://www.snowbomb.com/wp-login.php?action=lostpassword&redirect_to=http://www.snowbomb.com">Lost Password?</a>
</p>
</form>
相反这两条线:'R = requests.post( 'http://www.snowbomb.com/my-account-2',数据=信息) 请求= requests.get('HTTP ://www.snowbomb.com/my-account-2')',使用这个:'request = requests.get('http://www.snowbomb.com/my-account-2',auth = info) ' –
@ZeinabAbbasi我得到错误'TypeError:'字典'对象不可调用' – Liondancer
'如果'在request.content'中的SnowBome白金会员':是你在问题中提出的错字,还是你检查你的代码中有错误的字符串? –