簡體   English   中英

如何使用 Python 從 javascript 網頁刮取 HTML

[英]How to scrape HTML from a javascript webpage using Python

試圖解析 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>

如何從此 javascript 中獲取信息? 如何將其變成 html?

是的,你需要一個瀏覽器自動化(硒、無頭 Chrome)來執行現場 JS。 然后,JS 用缺失的數據填充 HTML。 例如。:

  1. https://webscraping.pro/javascript-rendering-library-for-scraping-javascript-sites/

  2. https://webscraping.pro/java-library-to-scrape-linkedin-its-data-affiliates/

哈克

在某些情況下,您可能會使用a bare coding (python、php)來模仿 JS 請求(通常是 XHR/Ajax)並獲取缺失的信息。 例如。 通過 Python 請求抓取 JS 延遲加載頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM