我是Python的新手,我尝试使用“请求”模块登录网站并打印出一些我的帐户信息。请参阅以下代码:
import requests
username = 'myusername'
password = 'mypassword'
URL = 'https://www.mmoga.com/login.php'
payload = {'email_address': username, 'password': password}
session = requests.session()
r = requests.post(URL, data=payload)
account = session.get('https://www.mmoga.com/shopping_cart.php')
print account.content
我是否还必须向requests.post
添加标题?
当我看到我看到的html源代码时
<div class="boxHeading">
<a href="mmoga.com/login.php"; title="- My Account -">- My Account -</a>
</div>
然而,我应该看到
<div class="boxHeading">
<a href="mmoga.com/account.php"; title="- My Account -">- My Account -</a>
</div>
答案 0 :(得分:1)
查看post登录网址似乎是:https://www.mmoga.com
我认为您也应该使用会话对象进行POST,以便凭据cookie实际存储在其中
r = session.post(URL,data = payload)
答案 1 :(得分:1)
在该页面上发布登录表单时,您还需要传递带有&#34; get&#34;的查询字符串。名为action
的参数:
import requests
username = 'myusername'
password = 'mypassword'
URL = 'https://www.mmoga.com/login.php'
payload = {'email_address': username, 'password': password}
params = {'action': 'process'}
session = requests.session()
r = session.post(URL, data=payload, params=params)
account = session.get('https://www.mmoga.com/shopping_cart.php')
print account.content
这应该可以解决登录问题。此外,登录期间未使用会话,因此您应使用session.post()
,如上所示。