使用DOM分析器在HTML头文件中获取<script>

时间:2018-10-08 07:33:49

标签: php html domparser

我目前正在为项目使用DOM分析器。另外,我在php中使用CURL抓取网站。我想从获取的HTML头部的script标签中获取一个值。但是我真的很困惑该怎么做。如果运行下面的代码:

  $ data_dom = new simple_html_dom();
$ data_dom-> load($ html);

foreach($ data_dom-> find('script')as $ script){
    回声$ script-> plaintext。“ 
”; }

结果是空值,当我检查它时,仅出现br标签。我想获得所有使用脚本标记的东西。这是头值:

  
我会给你我想要的脚本
.....


 

我想获取键变量。我将使用它来从网站获取数据。谢谢

1 个答案:

答案 0 :(得分:1)

根据其余标记的外观,您可以只使用DOMDocumentXPath,然后使用preg_match解析var的值。此示例将回显键。

<?php

$html = <<<END
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">
        var keysearch = {"departureLabel":"Surabaya (SUB : Juanda) Jawa Timur Indonesia","arrivalLabel":"Palangkaraya (PKY : Tjilik Riwut | Panarung) Kalimantan Tengah Indonesia","adultNum":"1","childNum":"0","infantNum":"0","departure":"SUB","arrival":"PKY","departDate":"20181115","roundTrip":0,"cabinType":-1,"departureCode":"ID-Surabaya-SUB","arrivalCode":"ID-Palangkaraya-PKY"};

        (function(window, _gtm, keysearch){

            if (window.gtmInstance){
                var departureExp = keysearch.departureCode.split("-");
                var arrivalExp = keysearch.arrivalCode.split("-");

                gtmInstance.setFlightData({
                    'ITEM_TYPE': 'flight',
                    'FLY_OUTB_CODE': departureExp[2],
                    'FLY_OUTB_CITY': departureExp[1],
                    'FLY_OUTB_COUNTRYCODE': departureExp[0],
                    'FLY_OUTB_DATE': keysearch.departDate,

                    'FLY_INB_CODE': arrivalExp[2],
                    'FLY_INB_CITY': arrivalExp[1],
                    'FLY_INB_COUNTRYCODE': arrivalExp[0],
                    'FLY_INB_DATE': keysearch.returnDate,
                    'FLY_NBPAX_ADL': keysearch.adultNum,
                    'FLY_NBPAX_CHL': keysearch.childNum,
                    'FLY_NBPAX_INF': keysearch.infantNum,
                });

                gtmInstance.pushFlightSearchEvent();
            }
        }(window, gtmInstance, keysearch));


                    var key = "rkey=10fe7b6fd1f7fa1ef0f4fa538f917811dbc7f4628a791ba69962f2ed305fb72d061b67737afd843aaaeeee946f1442bb";
                var staticRoot = 'http://sta.nusatrip.net';

        $(function() {
            $("#currencySelector").nusaCurrencyOptions({
                selected: getCookie("curCode"),
            });                        
        });
    </script>   
</head>
<body>foo</body>
</html>
END;


$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$result = $xpath->query('//script');

foreach($result as $currScriptTag)
{
    $currScriptContent = $currScriptTag->nodeValue;

    $matchFound = preg_match('/var key = "(.*)"/', $currScriptContent, $matches);

    if($matchFound)
    {
        /*
         * $matches[0] will contain the whole line like var key = "..." 
         * $matches[1] just contains the value of the var
         */
        $key = $matches[1];

        echo $key.PHP_EOL;
    }
}