I ran into a similar problem and the issue seems to be that calling script_tag.text
returns an empty string. Instead, you have to call script_tag.string
. Maybe this changed in some version of BeautifulSoup?
Anyway, @alecxe's answer didn't work for me, so I modified their solution:
import re
from bs4 import BeautifulSoup
data = """
<body>
<script>jQuery(window).load(function () {
setTimeout(function(){
jQuery("input[name=Email]").val("[email protected]");
}, 1000);
});</script>
</body>
"""
soup = BeautifulSoup(data, "html.parser")
script_tag = soup.find("script")
if script_tag:
# contains all of the script tag, e.g. "jQuery(window)..."
script_tag_contents = script_tag.string
# from there you can search the string using a regex, etc.
email = re.search(r'\.+val\("(.+)"\);', script_tag_contents).group(1)
print(email)
This prints [email protected]
.