2017-08-16 89 views
4

我试图登录本网站使用下面的请求,但它不工作如何使用Python请求登录此特定网站?

的饼干永远不会包含“用户ID”。

我应该改变什么?我需要在我的发布请求中添加标题吗?

import requests 

payload = { 
    'ctl00$MasterMainContent$LoginCtrl$Username': '[email protected]', 
    'ctl00$MasterMainContent$LoginCtrl$Password': 'mypassword', 
    'ctl00$MasterMainContent$LoginCtrl$cbxRememberMe' : 'on', 
} 

with requests.Session() as s: 
    login_page = s.get('http://www.bentekenergy.com/') 
    response = s.post('http://benport.bentekenergy.com/Login.aspx', data=payload) 
    if 'userid' in response.cookies: 
     print("connected") 
    else: 
     print("not connected") 

编辑1(以下评论): 我不知道要放什么东西在请求头,下面是我尝试过,但没有成功。

request_headers = { 
    'Accept':'image/webp,image/*,*/*;q=0.8', 
    'Accept-Encoding':'gzip, deflate, sdch, br', 
    'Accept-Language':'en-US,en;q=0.8', 
    'Connection':'keep-alive', 
    'Cookie':'ACOOKIE=C8ctADJmMTc1YTRhLTBiMTEtNGViOC1iZjE0LTM5NTNkZDVmMDc1YwAAAAABAAAASGYBALlflFnvWZRZAQAAAABLAAC5X5RZ71mUWQAAAAA-', 
    'Host':'statse.webtrendslive.com', 
    'Referer':'https://benport.bentekenergy.com/Login.aspx', 
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' 
} 

编辑2(以下stovfl答案):

我现在用以下的有效载荷,填充在表单中值的每个属性,并与用户名,密码和与rememberMe完成它。 我也试过在请求中使用下列标题。 还没连接

payload = { 
    '__VIEWSTATE' : '', 
    '__VIEWSTATEGENERATOR' : '', 
    '__PREVIOUSPAGE' : '', 
    '__EVENTVALIDATION' : '', 
    'isAuthenticated' : 'False', 
    'ctl00$hfAccessKey' : '', 
    'ctl00$hfVisibility' : '', 
    'ctl00$hfDateTime' : '', 
    'ctl00$hfHash' : '', 
    'ctl00$hfAnnouncementsUrl' : '', 
    'ctl00$MasterMainContent$LoginCtrl$Username' : '', 
    'ctl00$MasterMainContent$LoginCtrl$Password' : '', 
    'ctl00$MasterMainContent$LoginCtrl$cbxRememberMe' : '', 
} 

request_headers = { 
     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 
     'Accept-Encoding':'gzip, deflate, br', 
     'Accept-Language':'en-US,en;q=0.8', 
     'Cache-Control':'max-age=0', 
     'Connection':'keep-alive', 
     'Content-Length':'7522', 
     'Content-Type':'application/x-www-form-urlencoded', 
     'Cookie':'', 
     'Host':'benport.bentekenergy.com', 
     'Origin':'https://benport.bentekenergy.com', 
     'Referer':'https://benport.bentekenergy.com/Login.aspx', 
     'Upgrade-Insecure-Requests':'1', 
     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' 
} 

with requests.Session() as s: 
response = s.get('http://benport.bentekenergy.com/Login.aspx') 
soup = BeautifulSoup(response.text, "html.parser") 
if soup.find("input", {"name" : "ctl00$MasterMainContent$LoginCtrl$Username"}): 
    print("not connected") 
    soup = BeautifulSoup(response.text, "lxml") 
    for element in soup.select("input"): 
     if element.get("name") in payload: 
      payload[element.get("name")] = element.get("value") 

    payload['ctl00$MasterMainContent$LoginCtrl$Username'] = '[email protected]' 
    payload['ctl00$MasterMainContent$LoginCtrl$Password'] = 'mypassword' 
    payload['ctl00$MasterMainContent$LoginCtrl$cbxRememberMe'] = 'on' 

    response = s.post('http://benport.bentekenergy.com/Login.aspx', data=payload, headers=request_headers) 

    print (s.cookies) 
    soup = BeautifulSoup(response.text, "html.parser") 
    if soup.find("input", {"name" : "ctl00$MasterMainContent$LoginCtrl$Username"}): 
      print("not connected") 
    else: 
      print("connected") 

s.cookies包含:

<RequestsCookieJar[<Cookie BenportState=q1k2r2eqftltjm55igy5mg55 for .bentekenergy.com/>, <Cookie RememberMe=True for .bentekenergy.com/>]> 

编辑3(回答!):

我在有效载荷增加

'__EVENTTARGET' : '' 

,并填写它值'ctl00 $ MasterMainContent $ LoginCtrl $ btnSignIn'

现在我连接了! 注:标题是没有必要的,只是有效载荷

+0

是的,尝试添加标头 –

+0

我建议使用Selenium库进行此类任务。 –

+0

@AnkurSharma这是矫枉过正这种情况下... –

回答

1

评论:......发现有一个参数“__EVENTTARGET”这不是在有效载荷。它需要包含'ctl00 $ MasterMainContent $ LoginCtrl $ btnSignIn'。现在我连接了!

是,忽略了Submit Button,有一个Javascript

href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$headerLoginCtrl$btnSignIn&quot;, 

相关:SO Answer How To see POST Data


评论:......根据你的答案(编辑2 )。您正在使用http代替https
将自动重定向到https还没连接

  1. <RequestsCookieJar已经改变,所以一些进展。
    我仍不确定您的身份验证:if soup.find("input", {"name"...
    你检查页面内容?
    任何错误讯息?

  2. 不要使用BeautifulSoup(...您的以下要求应使用Session s重用分配Cookie
    例如response = s.get('<url to some resticted page>

  3. 尝试request_headers'User-Agent'


分析<形式>
登录网址:https://benport.bentekenergy.com/Login.aspx
形式:动作:/Login.aspx,方法:post

如果valueempty手段:预先设定值从登录页面。

1:input type:hidden value:/wEPDwUKLT... id:__VIEWSTATE 
2:input type:hidden value:0BA31D5D  id:__VIEWSTATEGENERATOR 
3:input type:hidden value:2gILTn0H1S... id:__PREVIOUSPAGE 
4:input type:hidden value:/wEWDAKIr6... id:__EVENTVALIDATION 
5:input type:hidden value:False   id:isAuthenticated 
6:input type:hidden value:nu66O9eqvE id:ctl00_hfAccessKey 
7:input type:hidden value:public  id:ctl00_hfVisibility 
8:input type:hidden value:08%2F16%2F... id:ctl00_hfDateTime 
9:input type:hidden value:3AB353573D... id:ctl00_hfHash 
10:input type:hidden value://announce... id:ctl00_hfAnnouncementsUrl 
11:input type:text  value:empty   id:ctl00_MasterMainContent_LoginCtrl_Username 
12:input type:password value:empty   id:ctl00_MasterMainContent_LoginCtrl_Password 
13:input type:checkbox value:empty   id:ctl00_MasterMainContent_LoginCtrl_cbxRememberMe 
+0

我刚刚根据你的回答编辑了我的帖子,反映了这些变化(编辑2)。仍未连接 –

+0

尝试使用标头和https尝试您的新建议:相同的结果。我也检查了页面的内容,在发布请求和s.get(')之后,我被重定向到了登录页面。 饼干已经改变,包含<曲奇BentekMarketingID = 1167161的.bentekenergy.com /> –

+0

我不知道如果我理解你的意思