简体   繁体   中英

Extract string from <script> - BeautifulSoup python

I'm trying to create a python script to extract some informations from a webmail. I wanna follow a redirection.

My code :

br1 = mechanize.Browser()
br1.set_handle_robots(False)
br1.set_cookiejar(cj)
br1.open("LOGIN URL")
br1.select_form(nr=0)
br1.form['username'] = mail_site
br1.form['password'] = pw_site
res1 = br1.submit()
html = res1.read()

print html

Result is not what i expect. It contains only a redirection script. I've seen that i have to extract the information from this script to follow this redirection. So, in my case,i've to extract jsessionid into a script.

The script is :

<script>
    function redir(){      
window.self.location.replace('/webmail/en_EN/continue.html;jsessionid=1D5QS4DA6C148DC4C14QS4CS5.1FDS5F4DSV1A64DA5DA?MESSAGE=NO_COOKIE&DT=1&URL_VALID=welcome.html');
return true;
}
</script>

If i'm not wrong, i've to build one regex. I've tried many things but no results.

Anyone have an idea ?

import re
get_jsession = re.search(r'jsessionid=([A-Za-z0-9.]+)',script_)
print(get_jsession.group(1))
>>> '1D5QS4DA6C148DC4C14QS4CS5.1FDS5F4DSV1A64DA5DA'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM