MasterAler

penenza.py

Apr 8th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import sys
  4. import os
  5. import requests
  6. from bs4 import BeautifulSoup
  7. import json
  8.  
  9. __LOGIN = "LOGIN" ### yours here
  10. __PASSWORD = "PASSWORD" ### yours here
  11. __PENENZA_OPTION = 17
  12. __LOGIN_URL = 'http://my.penenza.ru/main/sso/Login.aspx'
  13. __POST_LOGIN_URL = 'https://tender.otc.ru/main/sso/Handlers/PostLoginHandler.ashx'
  14. __HOME_URL = "http://my.penenza.ru/main/tradefinancemvc/Home"
  15. __LIST_URL = "https://my.penenza.ru/main/tradefinancemvc/FinOffer/List"
  16. __OFFERS = 'https://my.penenza.ru/main/tradefinancemvc/FinOffer/ExportToExcel?'
  17. __USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36"
  18.  
  19. def main():
  20.     login_data = {
  21.         'ctl00$MainContent$txtUserName': __LOGIN,
  22.         'ctl00$MainContent$txtUserPassword': __PASSWORD,
  23.         'ctl00$MainContent$ddlPlatforms': __PENENZA_OPTION,
  24.         'ctl00$MainContent$btnLogin': 'Войти',
  25.         'ctl00$MainContent$hidSignValue': '',
  26.         'ctl00$MainContent$hfCertExpDialogResult': ''
  27.         }
  28.     post_login_data = {
  29.         'IsCrmUser': 'False',
  30.         'RedirectUrl': 'https%3a%2f%2fmy.penenza.ru%2fmain%2fsso%2fdefault.aspx'
  31.     }
  32.     headers = {'User-Agent': __USER_AGENT }
  33.    
  34.     with requests.Session() as s:
  35.         s.headers.update(headers)
  36.        
  37.         page = s.get(__LOGIN_URL, verify=False)
  38.         soup = BeautifulSoup(page.content, "html.parser")
  39.         login_data["__VIEWSTATE"] = soup.select_one("#__VIEWSTATE")["value"]
  40.         login_data["__VIEWSTATEGENERATOR"] = soup.select_one("#__VIEWSTATEGENERATOR")["value"]
  41.         login_data["__EVENTVALIDATION"] = soup.select_one("#__EVENTVALIDATION")["value"]
  42.         first_response = s.post(__LOGIN_URL, data=login_data, verify=False)
  43.         print("---------------------------------------------")  
  44.        
  45.         mini_soup = BeautifulSoup(first_response.content, "html.parser")
  46.         post_login_data["SecurityToken"] = mini_soup.find("input", {"name": "SecurityToken"})["value"]
  47.         second_response = s.post(__POST_LOGIN_URL, data=post_login_data, verify=False)
  48.        
  49.         print("Status code:", second_response.status_code)
  50.            
  51.         with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "backoffice.html"), "w") as f:
  52.             f.write(second_response.content.decode('utf-8'))
  53.            
  54.         print("Well done")
  55.        
  56.  
  57.  
  58. if __name__ == "__main__":
  59.     main()
Add Comment
Please, Sign In to add comment