繁体   English   中英

页面中用于JavaScript代码的PHP简单HTML DOM解析器

[英]PHP Simple HTML DOM Parser for JavaScript Code Within the Page

我使用的是PHP Simple DOM解析器,HTML部分按预期工作,但是网页的某些内容是用JavaScript显示的。 JavaScript位于HTML页面中,而不是单独的文件,并且我需要的信息以纯文本格式清晰显示。 这是JavaScript代码的开始方式,所需的信息位于结尾(位置):

<script id="state">
    window.__INITIAL_STATE__ = {
        "detail": {
            "listingTopNavigation": {},
            "isTracked": false,
            "id": 30279925,
            "isImported": false,
            "isErotic": false,
            "isMessengerEnabled": false,
            "categories": [{
                "name": "Musique - Instruments",
                "id": 23,
                "isErotic": false
            }, {
                "name": "Guitares & Accessoires",
                "id": 697,
                "isErotic": false
            }, {
                "name": "Electrique",
                "id": 2354,
                "isErotic": false
            }],
            "imageData": {
                "baseUrl": "https:\u002F\u002Fcan01.anibis.ch\u002FElectrique-Line-6-DL-4-Delay-Modeler",
                "images": ["\u002F?[size]\u002Fanibis\u002F925\u002F279\u002F030\u002FLSmd7oN9LU6lAP5EbJQXXg_1.jpg", "\u002F?[size]\u002Fanibis\u002F925\u002F279\u002F030\u002Fhn2lNMOudUmuZofpM7wrsg_1.jpg", "\u002F?[size]\u002Fanibis\u002F925\u002F279\u002F030\u002FJKBbte21vEaD7ZJqcMi_Qg_1.jpg"]
            },
            "title": "Line 6 DL 4 Delay Modeler",
            "description": "\u003Cp\u003EVerkaufe mein Delay DL 6 in absolut neuwertigem Zustand wegen Nichtgebrauch. Das Delay habe ich im Dezember 2018 neu bei Musix gekauft und verfügt über eine tolle Auswahl an Delay Effekten, welche auf Fender Gibson Marshall oder Vox in Extraklasse daherkommen. Das Gerät ist optisch und technisch absolut neuwertig, kommt in Originalverpackung und mit dem passenden Netzadapter von Line 6. \u003C\u002Fp\u003E\n",
            "price": 190,
            "offerType": "Offer",
            "formattedPrice": "CHF 190.–",
            "formattedModified": "19.08.2019",
            "details": [{
                "name": "Remise",
                "value": "Retrait et envoi "
            }, {
                "name": "Type d’annonce",
                "value": "Offre"
            }],
            "location": {
                "country": "Suisse",
                "state": "Soleure",
                "zipCity": "4625 Oberbuchsiten (SO)"
            },

如何在PHP变量中获取这些值以供以后使用。

$country = $html->find(...);
$state = $html->find(...);
$zip_city = $html->find(...);

谢谢。

if ($html) {

    if ($html->find('script[id=state]', 0)) {

        $script = $html->find('script[id=state]', 0);
        $script = ltrim($script, '<script id="state">window.__INITIAL_STATE__=');
        $script = rtrim($script, '</script>');

        $obj = json_decode($script, true);

        if (isset($obj['detail']['contact']['phone'])) {

            $tel = $obj['detail']['contact']['phone'];
            $tel = ltrim($tel, '+41 ');


            if (isset($obj['detail']['location']['street']))
                $address = trim($obj['detail']['location']['street']);

            if (isset($obj['detail']['location']['zipCity']))
                $npa_ville = trim($obj['detail']['location']['zipCity']);
                $npa_ville = explode(' ', $npa_ville, 2);
                $npa = preg_replace('/[^0-9]/', '', $npa_ville[0]);
                $ville = $npa_ville[1];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM