I am trying to get the content of the variable 'html' from a javascript response. I am using the regex module to extract the html but I got 'None' as output.
response = 'var port_statistics = (function(window, undefined) {
function loadScript(url, callback) {
var script = document.createElement('script');
script.async = true;
script.src = url;
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(script, entry);
script.onload = script.onreadystatechange = function() {
var rdyState = script.readyState;
if (!rdyState || /complete|loaded/.test(script.readyState)) {
callback();
script.onload = null;
script.onreadystatechange = null;
}
};
}
function injectCss(css) {
var style = document.createElement('style');
style.type = 'text/css';
css = css.replace(/\}/g, "}\n");
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(style, entry);
}
var port_statistics = {};
var html = ["<div class=\"results_section\">", ", "
<div class='\"heading\"'> Overview </div> ",
#HERE THE CONTENT I AM TRYING TO GET
, "", "</div>", "", "", "</div>"].join('\n');
var div = document.createElement('div');
div.innerHTML = html;
var appendTo = document.getElementById('tag-port_statistics-widget');
appendTo.parentNode.insertBefore(div, appendTo);
loadScript('https://connect.url.com//jquery-1.11.1.min.js', function() {
portWidget.$(function () {
portWidget.$('tr.parent')
.click(function () {
portWidget.$(this).siblings('.child-' + this.id).fadeToggle('slow');
portWidget.$(this).find('.plus').toggle();
portWidget.$(this).find('.minus').toggle();
});
});
});
return port_statistics;
})(window);'
prog=re.search("var html = [.*?].join('\n');", response)
print(prog) #Output: None
I also tried this:
soup = BeautifulSoup(response, 'html.parser')
print(soup.prettify())
div_search = re.search('["<div class=\"results_section\">",(.*), "</div>"]', soup.prettify(), re.IGNORECASE)
print(div_search.group(0)) #Output: v
How can I do it to get the content of the variable 'html', please? In the second part, I would like to use this content to parse the content of the HTML tag with BeautifulSoup.
Thank you.
EDIT
I am trying to get this:
"<div class=\"results_section\">", ", "
<div class='\"heading\"'> Overview </div> ",
#HERE THE CONTENT I AM TRYING TO GET
, "", "</div>", "", "", "</div>"
result = re.search(r'var html = \[(.+?)\]', response, re.DOTALL)
print(result.group(1))
'.'
(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline .
Your text contain newline, you need use DOTALL to match.
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.