Home > front end >  Python Requests print HTML response variable
Python Requests print HTML response variable

Time:12-22

I use below script to get the temporary code from server

import requests
from bs4 import BeautifulSoup
payload{
'username':'demo',
'password':'demo'
}
with requests.session() as s:
    r= s.post(192.13.11.100,data=payload)
print(r.text)

No issues in script,

Now, I am getting the output as expected.

<html>
<body>
<script>
var session_key='d2e08508d3';
</script>
<script>
var temporary_data='01';
</script>
</body>
</html>

Now I wanted to get the session_key from the html output.

Please let me know how can I get the variable inside the html

CodePudding user response:

You could parse it using RegEx:

import re
regex = re.compile(".*?session_key\=\'(\S )\'")
session_key = regex.search(r.text).group(1)

Here you can test the regular expression further: RegExr

Here you can find some documentation on the search() method: re docs

CodePudding user response:

Trye this:

import re
from bs4 import BeautifulSoup

test_html = f"""
<html>
<body>
<script>
var session_key='d2e08508d3';
</script>
<script>
var temporary_data='01';
</script>
</body>
</html>
"""
soup = BeautifulSoup(test_html)
session_key = re.findall(r"session_key='(.*?)'", soup.find("script").text)[0]
print(session_key)

CodePudding user response:

According to this answer : Get JS var value in HTML source using BeautifulSoup in Python

You can do it :

from bs4 import BeautifulSoup
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor

data = """<html>
<body>
<script>
var session_key='d2e08508d3';
</script>
<script>
var temporary_data='01';
</script>
</body>
</html>"""

soup = BeautifulSoup(data, "html.parser")
script = soup.find("script", text=lambda text: text and "var session_key" in text)
parser = Parser()
tree = parser.parse(script.text)
for node in nodevisitor.visit(tree):
    if isinstance(node, ast.VarDecl) and node.identifier.value == 'session_key':
        print(node.initializer.value)

Please reward this answer for the work that he has done: https://stackoverflow.com/a/41020794/17077329

  • Related