Python与Scrapy和Selenium获取JavaScript生成内容

2024-04-19 20:08:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python和{}和{}来获取一些内容。网站HTML如下:

<html> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"> <body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0" onResize="resize();" bgcolor="#ffffff"> <iframe id="iframe" align="center" width="100%" height="100%" frameborder="0" marginWidth="0" marginHeight="0" src="" style="margin-top:1px;margin-left:1px;"></iframe> </body> <script> var eventTime = new Date(); function activeEvent() { eventTime = new Date(); } var height = 0; var width = 0; resize(); function setTitle(message) { document.title = message; } function resize() { height = document.body.clientHeight - 2; width = document.body.clientWidth - 2; if (height < 480) height = 480; if (width < 640) width = 640; document.getElementById("iframe").height = height; document.getElementById("iframe").width = width; } setInterval("resize()", 1000); document.getElementById("iframe").src = "http://xxxxxx.com:80/lhscm/framework/mainform/navui/nav3mainform.do?height=" + height + "&width=" + width; </script> </html>

但是使用硒driver.page_source不能得到src生成的HTML链接。
那么,如何获取内容?在


Tags: marginsrchttp内容varhtmlfunctionbody
1条回答
网友
1楼 · 发布于 2024-04-19 20:08:49

页面的主体似乎完全位于iframe内,因此要获得它,您应该切换到这个框架。尝试以下操作:

driver.switch_to_frame('iframe')
source = driver.page_source
driver.switch_to_default_content()

相关问题 更多 >