Search code examples
pythonhtmlbeautifulsouppython-requestspython-requests-html

Python Requests print HTML response variable


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


Solution

  • 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