有一些html中的數(shù)據(jù)是頁面加載完成后執(zhí)行的js代碼生成的蚯斯,源數(shù)據(jù)存儲(chǔ)在html的script中控妻,用php來獲取可以這樣寫:
? ? ? ? $html=<<<EOF
? ? ? ? ......
? ? <script>AA.BB = {
? ? "article_id": "20200511A041GF",
? ? "article_type": "0",
? ? "title": "美國確診超132萬奄侠!白宮稱副總統(tǒng)彭斯“目前沒有隔離”",
? ? "iNewsRecommendLevel": 1,
? ? "abstract": null,
? ? "catalog1": "politics",
? ? "catalog2": "politics_international",
? ? "introduction": "",
? ? "media": "央視新聞",
? ? "media_id": "58",
? ? "pubtime": "2020-05-11 08:09:07",
? ? "comment_id": "5221605433",
? ? "tags": "美國_社會(huì),白宮,彭斯,約翰斯·霍普金斯大學(xué),自我隔離",
? ? "content": null,
? ? "political": 1,
? ? "artTemplate": null,
? ? "FztCompetition": null,
? ? "FCompetitionName": null,
? ? "cms_id": "20200511A041GF00",
? ? "from_where": "sumeru-node",
? ? "videoArr": []
}</script>
? ? ? ? ......
EOF;
? ? ? ? $pattern='/AA\.BB\s*=\s*\{(.*?)\}/ms';
? ? ? ? preg_match($pattern,$html,$match);
? var_dump($match);
我們需要的數(shù)據(jù)再$match[1]中惠勒,給$match[1]中的數(shù)據(jù)兩邊加上花括號(hào)就是json格式的數(shù)據(jù)姚建,可以用json_decode來解析成數(shù)組方便使用
? ? ? ? $data=json_decode('{' . $match[1] . "}", true);
? ? ? ? var_dump($data);