【发布时间】:2014-11-21 04:32:36
【问题描述】:
我正在尝试编写一个简单的爬虫来获取我的 Internet 帐户的使用详细信息 - 我已经使用 Powershell 成功编写了它,但我想将其移至 Python 以便于使用/部署。如果我打印 r.text(POST 到登录页面的结果),我只会再次获取登录页面表单详细信息。
我认为解决方案可能类似于使用 prepare_request?如果我遗漏了一些非常明显的东西,我很抱歉,自从我接触 python 已经大约 5 年了^^
import requests
USERNAME = 'usernamehere'
PASSWORD = 'passwordhere'
loginURL = 'https://myaccount.amcom.com.au/ClientLogin.aspx'
secureURL = 'https://myaccount.amcom.com.au/FibreUsageDetails.aspx'
session = requests.session()
req_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
formdata = {
'ctl00$MemberToolsContent$txtUsername': USERNAME,
'ctl00$MemberToolsContent$txtPassword': PASSWORD,
'ctl00$MemberToolsContent$btnLogin' : 'Login'
}
session.get(loginURL)
r = session.post(loginURL, data=formdata, headers=req_headers, allow_redirects=False)
r2 = session.get(secureURL)
我在尝试中引用了这些线程:
HTTP POST and GET with cookies for authentication in python Authentication and python Requests
Powershell 脚本供参考:
$r=Invoke-WebRequest -Uri 'https://myaccount.amcom.com.au/ClientLogin.aspx' -UseDefaultCredentials -SessionVariable RequestForm
$r.Forms[0].Fields['ctl00$MemberToolsContent$txtUsername'] = "usernamehere"
$r.Forms[0].Fields['ctl00$MemberToolsContent$txtPassword'] = "passwordhere"
$r.Forms[0].Fields['ctl00$MemberToolsContent$btnLogin'] = "Login"
$response = Invoke-WebRequest -Uri 'https://myaccount.amcom.com.au/ClientLogin.aspx' -WebSession $RequestForm -Method POST -Body $r.Forms[0].Fields -ContentType 'application/x-www-form-urlencoded'
$response2 = Invoke-WebRequest -Uri 'https://myaccount.amcom.com.au/FibreUsageDetails.aspx' -WebSession $RequestForm
【问题讨论】:
-
它正在使用隐藏值 - 我正在准备答案
-
啊啊啊,Viewstate?我认为这将通过使用会话(如 Powershell 脚本)自动处理。我会尝试使用 BeautifulSoup 获取价值并在您准备实际答案时将其传递给 XD,感谢您的帮助!
-
完美!我已经发布了答案 - 发现它不仅仅是
VIEWSTATE,还有VIEWSTATEGENERATOR和RadMasterScriptManager_TSM -
明白了!答案确实是下面的@Md.Mohsin 脚本,但是“MemberToolsContent 用户名/密码字段中的 $ 不应转换为 %24
标签: python authentication python-requests scrape