-1

In a Get action I get the following http content

<!DOCTYPE html>\n<html lang=\'en\' dir=\'ltr\'>\n  <head>\n    <meta http-equiv="X-UA-Compatible" content="IE=edge" />\n    <title>VK | Entrar</title>\n    <link type="text/css" 
rel="stylesheet" href="https://vk.com/css/al/common.a3a0def732d3d99ddc92.css" /><link type="text/css" rel="stylesheet" href="https://vk.com/css/al/base.ae5502f141705b5f0c90.css" /><link type="text/css" rel="stylesheet" href="https://vk.com/css/al/fonts_utf.53f24c4c5577419ba632.css" /><link type="text/css" rel="stylesheet" href="https://vk.com/css/al/fonts_cnt.7cc314ac69198e349896.css" />\n    <link type="text/css" rel="stylesheet" href="https://vk.com/css/api/oauth_popup.98ea88a667e23b0d6ec1.css" />\n    <script type="text/javascript" language="javascript" src="https://vk.com/js/api/common_light.js?2"></script>\n    <script type="text/javascript" language="javascript">// <![CDATA[\n      function allow(button) 
{\n        if (isButtonLocked(button)) return false;\n      
  lockButton(button);\n\n        var addr = \'\';\n        if (isChecked(\'allow_notifications\')) {\n          addr = \'&notify=1\';\n        }\n        if (isChecked(\'denied_email\')) {\n          addr += \'&email_denied=1\';\n        }\n        if (isChecked(\'denied_phone\')) {\n          addr 
+= \'&phone_denied=1\';\n        }\n        location.href = 
"https://login.vk.com/?act=grant_access&client_id=735437&settings=65538&response_type=token&group_ids=&token_type=0&v=5.69&display=page&ip_h=easdasd3ss9a974&hash=1606850344_eeaa68bd16d875c7a5&https=1&state=&redirect_uri=https%3A%2F%2Fauth.hyd%2Foauth%2Flogin%3Fclient_id%3D10058%26redirect_uri%3Dhttps%253A%252F%.live%252Flogin%26response_type%3Dtoken%26platform%3D5%26locale%3Den-GB"+addr;\n      
  return false;\n      }\n\n      function cancel() {\n     
   location.href = "https://login.vk.com/?act=grant_access&client_id=735437&settings=65538&response_type=token&group_ids=&token_type=0&v=5.69&display=page&ip_h=asdasdaas74&hash=1606850344_eeaa68bd16d875c7a5&https=1&state=&redirect_uri=https%3A%2F%2Fauth.live%2Foauth%2Flogin%3Fclient_id%3D10058%26redirect_uri%3Dhttps%253A%252F%.live%252Flogin%26response_type%3Dtoken%26platform%3D5%26locale%3Den-GB&cancel=1";\n        return false;\n      }\n\n      function login(button) {\n        if (isButtonLocked(button)) return false;\n        lockButton(button);\n        ge(\'login_submit\').submit();\n      }\n\n      function doResize(onResize) {\n        onResize && setTimeout(function() {\n    
      doResize()\n        }, 100);\n\n        if (!hasClass(document.body, \'oauth_centered\') && !onResize) {\n        
  if (window.outerHeight !== void 0) {\n            var panelH = (window.outerHeight - window.innerHeight) | 0,\n       
       panelW = (window.outerWidth - window.innerWidth) | 0;\n          } else {\n            var panelH = 50,\n        
      panelW = 0;\n          }\n          var contentH = Math.max(ge(\'oauth_wrap_content\').offsetHeight, 430),\n      
      contentW = 655;\n          window.resizeTo(contentW + 
panelW, contentH + panelH);\n          window.moveTo(\n     
       (screen.width - contentW) / 2 + (screen.availLeft | 0),\n            ((screen.height - contentH) / 2) + (screen.availTop | 0)\n          );\n        }\n      }\n\n      function toggleEmailPrivacy() {\n        checkbox(\'denied_email\');\n        if (!isChecked(\'denied_email\')) {\n          hide(\'denied_email\');\n          show(\'allowed_email\');\n        } 
else {\n          hide(\'allowed_email\');\n          show(\'denied_email\');\n        }\n      }\n\n      function togglePhonePrivacy() {\n        checkbox(\'denied_phone\');\n        if (!isChecked(\'denied_phone\')) {\n          hide(\'denied_phone\');\n          show(\'allowed_phone\');\n        } else {\n
 hide(\'allowed_phone\');\n          show(\'denied_phone\');\n  
      }\n      }\n\n      if (parent && parent != window) {\n   
     location.href = "https://oauth.vk.com/blank.html";\n      }\n\n      \n    // ]]></script>\n  </head>\n\n  <body onload="doResize();" class="VK1 oauth_centered">\n    <script>\n      if (window.devicePixelRatio >= 2) document.body.className += \' is_2x\';\n    </script>\n    <div class="oauth_wrap">\n      <div class="oauth_wrap_inner">\n        <div class="oauth_wrap_content" id="oauth_wrap_content">\n          <div class="oauth_head">\n 
 <a class="oauth_logo fl_l" href="https://vk.com" target="_blank"></a>\n  <div id="oauth_head_info" class="oauth_head_info fl_r">\n  <a class="oauth_reg_link" href="https://vk.com/join?reg=1" 
target="_blank">Registrar</a>\n</div>\n</div>\n\n<div class="oauth_content box_body clear_fix">\n  <div class="box_msg_gray box_msg_padded">Voc&#234; deve fazer login no <b>VK</b> para proceder.</div>\n\n  <form method="POST" id="login_submit" action="https://login.vk.com/?act=login&soft=1">\n    <div class="oauth_form">\n\n      \n\n      <div class="oauth_form_login">\n        <input type="hidden" name="ip_h" value="e1534232dddd9a974" />\n  
      <input type="hidden" name="lg_h" value="a52943232345d210ca" />\n        <input type="hidden" name="_origin" value="https://oauth.vk.com" />\n        <input type="hidden" name="to" value="aHR0cHM6Ly9vYXV0aC52ay5jb20vYXV0aG9yaXplP2NsaWVudF9pZD03MzIwNDE3JnJlZGlyZWN0X3VyaT1odHRwcyUzQSUyRiUyRmF1dGguYm9veWFo324332243423mxvZ2luJTNGY2xpZW50X2lkJTNEMTAwNTglMjZyZWRpcmVjdF91cmklM0RodHRwcyUyNTNBJTI1MkYlMjUyRmJvb3lhaC5saXZlJTI1MkZsb2dpbiU23423423dwsfdsfsdPSZkaXNwbGF5PXBhZ2U-" />\n        <input type="hidden" id="expire" name="expire" value="0" />\n\n        <div class="oauth_form_header">Telefone ou email</div>\n        <input type="text" class="oauth_form_input dark" name="email" value="">\n        <div 
class="oauth_form_header">Senha:</div>\n        <input type="password" class="oauth_form_input dark" name="pass" />\n\n        \n\n        <button class="flat_button oauth_button button_wide" 
id="install_allow" type="submit" onclick="return login(this);">Entrar</button>\n        <a class="oauth_forgot" href="https://vk.com/restore" target="_blank">Esqueceu sua senha?</a>\n        <input type="submit" name="submit_input" class="unshown">\n      
</div>\n    </div>\n  </form>\n</div>\n        </div>\n      </div>\n    </div>\n  </body>\n</html>'

I need to get some specific values that are (The Values I need to Obtain will be highlighted in Bold)

<input type="hidden" name="ip_h" value="e1534232dddd9a974" />\n

<input type="hidden" name="lg_h" value="a52943232345d210ca" />\n

<input type="hidden" name="to" value="aHR0cHM6Ly9vYXV0aC52ay5jb20vYXV0aG9yaXplP2NsaWVudF9pZD03MzIwNDE3JnJlZGlyZWN0X3VyaT1odHRwcyUzQSUyRiUyRmF1dGguYm9veWFo324332243423mxvZ2luJTNGY2xpZW50X2lkJTNEMTAwNTglMjZyZWRpcmVjdF91cmklM0RodHRwcyUyNTNBJTI1MkYlMjUyRmJvb3lhaC5saXZlJTI1MkZsb2dpbiU23423423dwsfdsfsdPSZkaXNwbGF5PXBhZ2U-" />\n

I will need to use this method a lot to get a specific word, if I can, I kindly ask you to leave me an explanation on how I can do it without errors, in addition I will leave my sample code if I can just complete it with the way to get these values would be very grateful. Thanks

Script example

import requests
import json


url= 'site.com'

header = {
            '.....': '.......',
          }

request = requests.get(url, headers=header)

print(request.status_code)
print(request.content)
James Z
  • 11,838
  • 10
  • 25
  • 41
CartelWeb
  • 3
  • 1
  • 1
    So... have you actually tried something? What went wrong? Please include your best effort, because your current "script example" is missing everything for the part you're asking about – James Z Dec 01 '20 at 20:36
  • Sorry... This is the correct duplicate: https://stackoverflow.com/q/30489296 – Robert Harvey Dec 01 '20 at 20:41

1 Answers1

0

You should use the bs4 and lxml package to parse the HTML from your response.

Here is how you can get the values you asked for:

import requests
from bs4 import BeautifulSoup

request_url = 'http://example.com'

response = requests.get(url)

page = BeautifulSoup(response.content, 'lxml')

ip_h = page.select_one('[name="ip_h"]').get('value')
lg_h = page.select_one('[name="lg_h"]').get('value')
to = page.select_one('[name="to"]').get('value')
Anatol
  • 1,617
  • 1
  • 8
  • 20