【问题标题】:Access authenticated page using Python Requests使用 Python 请求访问经过身份验证的页面
【发布时间】: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,还有VIEWSTATEGENERATORRadMasterScriptManager_TSM
  • 明白了!答案确实是下面的@Md.Mohsin 脚本,但是“MemberToolsContent 用户名/密码字段中的 $ 不应转换为 %24

标签: python authentication python-requests scrape


【解决方案1】:
import requests
import re
from bs4 import BeautifulSoup

user="xyzmohsin"
passwd="abcpassword"

s=requests.Session()
headers={"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"}
s.headers.update(headers)

login_url="https://myaccount.amcom.com.au/ClientLogin.aspx"
r=s.get(login_url)
soup=BeautifulSoup(r.content)
RadMasterScriptManager_TSM=soup.find(src=re.compile("RadMasterScriptManager_TSM"))['src'].split("=")[-1]
EVENTTARGET=soup.find(id="__EVENTTARGET")['value']
EVENTARGUMENT=soup.find(id="__EVENTARGUMENT")['value']
VIEWSTATE=soup.find(id="__VIEWSTATE")['value']
VIEWSTATEGENERATOR=soup.find(id="__VIEWSTATEGENERATOR")['value']


data={"RadMasterScriptManager_TSM":RadMasterScriptManager_TSM,
"__EVENTTARGET":EVENTTARGET,
"__EVENTARGUMENT":EVENTARGUMENT,
"__VIEWSTATE":VIEWSTATE,
"__VIEWSTATEGENERATOR":VIEWSTATEGENERATOR,
"ctl00_TopMenu_RadMenu_TopNav_ClientState":"",
"ctl00%24MemberToolsContent%24HiddenField_Redirect":"",
"ctl00%24MemberToolsContent%24txtUsername":user,
"ctl00%24MemberToolsContent%24txtPassword":passwd,
"ctl00%24MemberToolsContent%24btnLogin":"Login"}

headers={"Content-Type":"application/x-www-form-urlencoded",
"Host":"myaccount.amcom.com.au",
"Origin":"https://myaccount.amcom.com.au",
"Referer":"https://myaccount.amcom.com.au/ClientLogin.aspx"}

r=s.post(login_url,data=data,headers=headers)

我没有用户名和密码,因此无法测试最终发布请求中的标题。如果它不起作用 - 那么请从最终发布请求的标题中删除 HostOriginReferer

希望有所帮助:-)

【讨论】:

  • @Mike Anthony。我已经更新了代码。之前错过了一条线。您在上面看到的应该可以正常工作。让我知道它是否有帮助:-)
  • 啊哈,谢谢!在这里,我拉着头发想知道为什么它仍然不起作用哈哈。立即测试
  • @Mike Anthony 让我知道会发生什么:-)
  • 嗯,r.text 包含:“在尝试处理您的登录时发生以下错误。
    请确保所有字段都填写正确格式”。我现在正在比较我在 powershell 和 python 中发送的字段
  • @ Mike Anthony 您是否按原样尝试代码 - 除了用户和密码?还是你改变了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
相关资源
最近更新 更多