簡體   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