-1

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

3
  • What have you tried? Did you check library like Beautiful Soup? Commented Dec 22, 2021 at 8:16
  • You could try regex with r"session_key=['|\"]{1}([0-9a-zA-Z]{1,})['|\"]{1}" on the html string and retrieve group 1 Commented Dec 22, 2021 at 8:16
  • @benjababe, although regex MAY work in some very limited number of cases, it's better not to use regex to parse HTML. But you can use regex, ONCE the text from the tag is extracted and then to obtain the specific value. Commented Dec 22, 2021 at 8:18

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.