2
我正在嘗試使用Python腳本登錄網站,存儲我收到的cookie,然後使用該cookie訪問網站的會員專用部分。我已閱讀了幾篇關於此主題的文章和答案,但沒有任何答案適用於我。如何使用Python請求登錄網站,存儲cookie,然後訪問網站上的另一個頁面?
以下是我嘗試訪問的網站登錄頁面的HTML代碼。
<form action="/login?task=user.login" method="post">
<fieldset>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="70" nowrap="">Username </td>
<td width="260"><input type="text" name="username" id="username" value="" class="validate-username" size="25"/></td>
</tr>
<tr>
<td width="70" nowrap="">Password </td>
<td width="260"><input type="password" name="password" id="password" value="" class="validate-password" size="25"/></td>
</tr>
<tr>
<td colspan="2"><label style="float: left;width: 70%;" for="modlgn_remember">Remember Me</label>
<input style="float: right;width: 20%;"id="modlgn_remember" type="checkbox" name="remember" class="inputbox" value="yes"/></td>
</tr>
<tr>
<td colspan="2" width="100%"> <a href="/reset-password"> Forgot your password?</a></td>
</tr>
<tr>
<td colspan="2" width="100%"> <a href="/username-reminder">Forgot your username?</a></td>
</tr>
<tr>
<td colspan="2"><button type="submit" class="button cta">Log in</button></td>
<!-- <td colspan="1"><a href="/--><!--">Register Now</a></td>-->
</tr>
</tbody>
</table>
<input type="hidden" name="return"
value="aHR0cHM6Ly9maWYuY29tLw=="/>
<input type="hidden" name="3295f23066f7c6ab53c290c6c022cc4b" value="1" /> </fieldset>
</form>
這是我自己的代碼,我用來嘗試登錄。
from requests import session
payload = {
'username': 'MY_USERNAME',
'password': 'MY_PASSWORD'
}
s = session()
s.post('https://fif.com/login?task=user.login', data=payload)
response = s.get('https://fif.com/tools/capacity')
從我讀過的一切,這應該工作,但它沒有。我一直在爲此掙扎兩天,所以如果你知道答案,我會喜歡這個解決方案。
僅供參考,這裏都是我在希望看着一個答案其他StackOverflow的帖子:
- Python Requests and Persistent Sessions
- Logging into a site using Python Reqeusts
- Login to website using python
- How to 「log in」 to a website using Python's Requests module?
- Python: Requests Session Login Cookies
- How to use Python to login to a webpage and retrieve cookies for later usage?
- cUrl Login then cUrl Download
只是爲了澄清,我不應該使用所有這些用戶 - 只有其中一個,正確的嗎?而你的意思是「你應該發佈所有必要的數據」?我錯過了什麼? – Jacob
@Jacob,只有一個 –
你是什麼意思「你應該發佈所有需要的數據」?我錯過了什麼? @Padraic – Jacob