-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

Moses
  • 214
  • 1
  • 4
  • 12
  • What have you tried? Did you check library like [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)? – buran Dec 22 '21 at 08:16
  • You could try regex with r"session_key=['|\"]{1}([0-9a-zA-Z]{1,})['|\"]{1}" on the html string and retrieve group 1 – benjababe Dec 22 '21 at 08:16
  • @benjababe, although regex MAY work in some very limited number of cases, it's [better not to use regex to parse HTML](https://stackoverflow.com/a/1732454/4046632). But you can use regex, ONCE the text from the tag is extracted and then to obtain the specific value. – buran Dec 22 '21 at 08:18

3 Answers3

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

Y. Georgiev
  • 580
  • 5
  • 13
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)
Xu Qiushi
  • 1,111
  • 1
  • 5
  • 10
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

év3v
  • 26
  • 4