我想用我的用戶名和密碼登錄網站,但我似乎無法得到它的工作。現在我正在使用該網站進行測試。如果我能夠登錄,我應該能夠找到說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白金會員':是你在問題中提出的錯字,還是你檢查你的代碼中有錯誤的字符串? –