如何使用Python从javascript网页中提取HTML

如何使用Python从javascript网页中提取HTML,javascript,python,html,linux,screen-scraping,Javascript,Python,Html,Linux,Screen Scraping,试图解析html以便从嵌套在标记中的标记中获取数据,但是当我美化时,我得到了javascript。如何从这个javascript中获取信息?如何将其转换为html?有没有更好的方法来获取这些信息?这是我的第一个问题,如果我犯了任何错误,我道歉。多谢各位 这是我的代码: from bs4 import BeautifulSoup as bs import requests html = requests.get(url) soup = bs(html.content, 'html.parser'

试图解析html以便从嵌套在标记中的标记中获取数据,但是当我美化时,我得到了javascript。如何从这个javascript中获取信息?如何将其转换为html?有没有更好的方法来获取这些信息?这是我的第一个问题,如果我犯了任何错误,我道歉。多谢各位

这是我的代码:

from bs4 import BeautifulSoup as bs
import requests

html = requests.get(url)
soup = bs(html.content, 'html.parser')
print(soup.prettify())
答复是: 预修饰代码的字节/字符串后跟

<html>
<head>
</head>
<script language="javascript">
var strUrl = window.location.href;


if (strUrl.indexOf("modisoftinc.com") > 0)
    window.location.replace("https://www.modisoftinc.com/home.html");
if (strUrl.indexOf("www.modisoftinc.com") > 0)
    window.location.replace("https://www.modisoftinc.com/home.html");
if (strUrl.indexOf("http://modisoftinc.com") > 0)
    window.location.replace("https://www.modisoftinc.com/home.html");
if (strUrl.indexOf("www.modisoftinc.com") > 0)
    window.location.replace("https://www.modisoftinc.com/home.html");


if (strUrl.indexOf("echecks.modisoftinc.com") > 0)
    window.location.replace("https://echecks.modisoftinc.com/Account/Logon");


if (strUrl.indexOf("pos.modisoftinc.com") > 0)
    window.location.replace("https://pos.modisoftinc.com/Account/Logon");


if (strUrl.indexOf("clock.modisoftinc.com") > 0)
    window.location.replace("https://clock.modisoftinc.com/Account/Logon");


if (strUrl.indexOf("admin11.modisoftinc.com") > 0)
    window.location.replace("https://admin11.modisoftinc.com/Account/Logon");




if (strUrl.indexOf("modisoft.com") > 0)
    window.location.replace("https://www.modisoft.com/home.html");
if (strUrl.indexOf("www.modisoft.com") > 0)
    window.location.replace("https://www.modisoft.com/home.html");
if (strUrl.indexOf("http://modisoft.com") > 0)
    window.location.replace("https://www.modisoft.com/home.html");
if (strUrl.indexOf("www.modisoft.com") > 0)
    window.location.replace("https://www.modisoft.com/home.html");


if (strUrl.indexOf("echecks.modisoft.com") > 0)
    window.location.replace("https://echecks.modisoft.com/Account/Logon");

if (strUrl.indexOf("app.modisoft.com") > 0)
    window.location.replace("https://app.modisoft.com/Account/Logon");

if (strUrl.indexOf("app1.modisoft.com") > 0)
    window.location.replace("https://app1.modisoft.com/Account/Logon");

if (strUrl.indexOf("app2.modisoft.com") > 0)
    window.location.replace("https://app2.modisoft.com/Account/Logon");

if (strUrl.indexOf("pos.modisoft.com") > 0)
    window.location.replace("https://pos.modisoft.com/Account/Logon");

if (strUrl.indexOf("clock.modisoft.com") > 0)
    window.location.replace("https://clock.modisoft.com/Account/Logon");

    if (strUrl.indexOf("admin11.modisoft.com") > 0)
    window.location.replace("https://admin11.modisoft.com/Account/Logon");



if (strUrl.indexOf("modisoftrewards.com") > 0)
    window.location.replace("https://www.modisoftrewards.com/index.html");
if (strUrl.indexOf("www.modisoftrewards.com") > 0)
    window.location.replace("https://www.modisoftrewards.com/index.html");
if (strUrl.indexOf("http://modisoftrewards.com") > 0)
    window.location.replace("https://www.modisoftrewards.com/index.html");
if (strUrl.indexOf("www.modisoftrewards.com") > 0)
    window.location.replace("https://www.modisoftrewards.com/index.html");






   if (strUrl.indexOf("localhost") > 0)
       window.location.replace("Account/Logon");
</script>
<body>
</body>
</html>

var strUrl=window.location.href;
如果(strUrl.indexOf(“modisoftinc.com”)>0)
window.location.replace(“https://www.modisoftinc.com/home.html");
如果(strUrl.indexOf(“www.modisoftinc.com”)>0)
window.location.replace(“https://www.modisoftinc.com/home.html");
如果(结构索引)http://modisoftinc.com") > 0)
window.location.replace(“https://www.modisoftinc.com/home.html");
如果(strUrl.indexOf(“www.modisoftinc.com”)>0)
window.location.replace(“https://www.modisoftinc.com/home.html");
如果(strUrl.indexOf(“echecks.modisoftinc.com”)>0)
window.location.replace(“https://echecks.modisoftinc.com/Account/Logon");
如果(strUrl.indexOf(“pos.modisoftwinc.com”)>0)
window.location.replace(“https://pos.modisoftinc.com/Account/Logon");
如果(strUrl.indexOf(“clock.modisoftinc.com”)>0)
window.location.replace(“https://clock.modisoftinc.com/Account/Logon");
如果(strUrl.indexOf(“admin11.modisoftinc.com”)>0)
window.location.replace(“https://admin11.modisoftinc.com/Account/Logon");
如果(strUrl.indexOf(“modisoft.com”)>0)
window.location.replace(“https://www.modisoft.com/home.html");
如果(strUrl.indexOf(“www.modisoft.com”)>0)
window.location.replace(“https://www.modisoft.com/home.html");
如果(结构索引)http://modisoft.com") > 0)
window.location.replace(“https://www.modisoft.com/home.html");
如果(strUrl.indexOf(“www.modisoft.com”)>0)
window.location.replace(“https://www.modisoft.com/home.html");
如果(strUrl.indexOf(“echecks.modisoft.com”)>0)
window.location.replace(“https://echecks.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“app.modisoft.com”)>0)
window.location.replace(“https://app.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“app1.modisoft.com”)>0)
window.location.replace(“https://app1.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“app2.modisoft.com”)>0)
window.location.replace(“https://app2.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“pos.modisoft.com”)>0)
window.location.replace(“https://pos.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“clock.modisoft.com”)>0)
window.location.replace(“https://clock.modisoft.com/Account/Logon");
if(strUrl.indexOf(“admin11.modisoft.com”)>0)
window.location.replace(“https://admin11.modisoft.com/Account/Logon");
如果(strUrl.indexOf(“modisoftrewards.com”)>0)
window.location.replace(“https://www.modisoftrewards.com/index.html");
if(strUrl.indexOf(“www.modisoftrewards.com”)>0)
window.location.replace(“https://www.modisoftrewards.com/index.html");
如果(结构索引)http://modisoftrewards.com") > 0)
window.location.replace(“https://www.modisoftrewards.com/index.html");
if(strUrl.indexOf(“www.modisoftrewards.com”)>0)
window.location.replace(“https://www.modisoftrewards.com/index.html");
if(strUrl.indexOf(“localhost”)>0)
窗口。位置。替换(“帐户/登录”);
如何从这个javascript中获取信息?如何将其转换为html

是的,您需要浏览器自动化(selenium,headless Chrome)来执行现场JS。然后,JS用丢失的数据填充HTML。 例如:

  • 乱劈
    在某些情况下,您可能会使用
    一个简单的编码
    (python,php)来模拟JS请求(通常是XHR/Ajax)并获取缺少的信息。你不能把它转换成html。根据您对页面的需求,您将需要自动化浏览器,让javascript在页面上运行,然后获取您想要的内容,或者使用网络选项卡/网络监控工具查看xhr是否可以从另一个uri获取您想要的内容,并向该端点发出请求