I need to extract the src-link of an iframe with python. But I have 2 problems:
- The iframe only appears after clicking on a button/link.
- The iframe has no src, but inside the iframe tags is a #document. Inside is another iframe with the src link
Problem 1:
This is the relevant HTML code snipped before clicking the button:
<div class="video-content" id="video">
<div class="autosize-container">
<a href="#stream" id="stream" title="xyz"><img src="/img/tem/image.png"></a>
</div>
</div>
The button/link:
<a href="#video" data-mirror="1" data-host="1" title="xyz">xyz</a>
has these two events (on click):
function() {
$.ajax("", {
type: "POST",
async: !1,
data: {
req: "1",
pid: "57315",
mirror: $(this).data("mirror"),
host: $(this).data("host"),
rel: $("#rel option:selected").attr("data")
},
success: function(e) {
$(".autosize-container").html(e)
}
})
}
and
function(b) {
return "undefined" != typeof n && n.event.triggered !== b.type ? n.event.dispatch.apply(a, arguments) : void 0
}
This is the same HTML code snipped after clicking the button:
<div class="video-content" id="video">
<div class="autosize-container">
<form id="moodleform" target="iframe" method="post" action="">
<input type="hidden" name="req" value="2">
<input type="hidden" name="pid" value="57315">
<input type="hidden" name="mirror" value="1">
<input type="hidden" name="host" value="1">
<input type="hidden" name="rel" value="71580,0">
</form>
<iframe name="iframe" scrolling="no" frameborder="0" width="100%" height="480px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" style="background-color:#eee"></iframe>
<script>
document.getElementById('moodleform').submit();
</script>
</div>
</div>
(The ajax_data values can be found in the html code before clicking any button. So I used them to emit the ajax:)
With my current python code, I might have worked around the first problem:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
ajax_url = "https://website.com"
ajax_data = {
"req": "1",
"pid": "57316",
"mirror": "1",
"host": "1",
"rel": "71584,0"
}
response = requests.post(ajax_url, data=ajax_data, headers=headers)
print(response.text)
Output:
<form id="moodleform" target="iframe" method="post" action="">
<input type="hidden" name="req" value="2" />
<input type="hidden" name="pid" value="57316" />
<input type="hidden" name="mirror" value="1" />
<input type="hidden" name="host" value="1" />
<input type="hidden" name="rel" value="71584,0" />
</form>
<iframe name="iframe" scrolling="no" frameborder="0" width="100%" height="480px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" style="background-color:#eee"></iframe>
<script>
document.getElementById('moodleform').submit();
</script>
Now I get the HTML code snipped that I would get after clicking the button on the website if I’m in the edit mode (see problem 2).
Problem 2:
But now the problem is that in the browser dev tools, inside the iframe is #document (<iframe […]>#document). After expanding it, there is more code and also an iframe with the src link.
However, if I click on “edit html” in the browser dev tools, this #document disappears. But if I expand it in the dev-tools, I can look inside and copy the link.
In my python code #document cannot be found either.
Now my question: How can I get the scr-link from the iframe within #document with python?
Inside the #document is more html and also the src-link i am searching for:
<iframe src="https://example.com" scrolling="no" frameborder="0" width="100%" height="480px" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
#document inside iframe. Contains an iframe with the wanted src link:

document disappeared in edit mode (right click -> edit html):
