-->

在PHP解析HTML和返回JSON(Parse HTML in PHP and return JSO

2019-10-19 05:27发布

我使用PHP简单的HTML DOM解析器在我的PHP脚本来分析来自网站的信息转换成JSON对象。 我的JSON对象到底应该采用以下格式:

阵列最多5个对象(周一至周五)或更小(周二至周五等)。

所有这些对象应该有两个阵列,一个叫food1和一个叫food 2 。 这两个数组应该包含多个食品名称和价格。 我认为,在JSON它看起来是这样的:

    {
  "day" : [
    {
      "food1" : [
        {
          "price" : "1.00",
          "foodname" : "test"
        },
        {
          "price" : "1.00",
          "foodname" : "test"
        }
      ],
      "food2" : [
        {
          "price" : "2.00",
          "foodname" : "test2"
        },
        {
          "price" : "2.00",
          "foodname" : "test2"
        }
      ]
    },
    {
      "food1" : [
        {
          "price" : "1.00",
          "foodname" : "test"
        },
        {
          "price" : "1.00",
          "foodname" : "test"
        }
      ],
      "food2" : [
        {
          "price" : "2.00",
          "foodname" : "test2"
        },
        {
          "price" : "2.00",
          "foodname" : "test2"
        }
      ]
    },
    {
      "food1" : [
        {
          "price" : "1.00",
          "foodname" : "test"
        },
        {
          "price" : "1.00",
          "foodname" : "test"
        }
      ],
      "food2" : [
        {
          "price" : "2.00",
          "foodname" : "test2"
        },
        {
          "price" : "2.00",
          "foodname" : "test2"
        }
      ]
    },
    {
      "food1" : [
        {
          "price" : "1.00",
          "foodname" : "test"
        },
        {
          "price" : "1.00",
          "foodname" : "test"
        }
      ],
      "food2" : [
        {
          "price" : "2.00",
          "foodname" : "test2"
        },
        {
          "price" : "2.00",
          "foodname" : "test2"
        }
      ]
    },
    {
      "food1" : [
        {
          "price" : "1.00",
          "foodname" : "test"
        },
        {
          "price" : "1.00",
          "foodname" : "test"
        }
      ],
      "food2" : [
        {
          "price" : "2.00",
          "foodname" : "test2"
        },
        {
          "price" : "2.00",
          "foodname" : "test2"
        }
      ]
    }
  ]
}

反正我以前只用Objective-C和PHP中解决这个问题有问题的工作。 我还实施在Objective-C是工作的解析器,但如果网站改变它们的结构,我将不得不重新提交整个应用程序等,这就是为什么我要拍一个Web服务,我可以动态改变之外解析器应用程序。 我得到的是这样的:

<?php
include('simple_html_dom.php');

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$html = file_get_html('http://www.studentenwerk-karlsruhe.de/de/essen/?view=ok&STYLE=popup_plain&c=erzberger&p=1&kw=3',false,$context);

foreach($html->find('b') as $e) 
    echo $e;

?>

这给了我所有的食物名称,但它是没有排序的天,也没有对不同食物的菜单(也有每一天两个不同的菜单被称为food1food2在我的例子JSON对象)。

在我的Objective-C的解析器我刚刚创建了一个新的一天对象的时候,食品的名称是“SchniPoSa”,并补充以下所有的食物名称food1 ,直到有自带食物的名称“Salatbuffet”这一点,下面所有的食物我加名food2阵列,直到出现的下一个“SchniPoSa”食品名称。 但是,这是不是很好,因为该结构可以每天换。

另外,我甚至不知道如何实现在PHP。 在我小的PHP脚本我也不解析所有这些都是标签价格<span class="bgp price_1">

下面是我想分析的信息的网站:

http://www.studentenwerk-karlsruhe.de/de/essen/?view=ok&STYLE=popup_plain&c=erzberger&p=1&kw=3

是否有任何人谁可以帮我像下面我描述了一个有效的JSON对象解析信息?

Answer 1:

刚看到你的消息,意识到我还没有得到回复你这个问题。 也许这会导致你在正确的方向:

<?php

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$html = file_get_contents('http://www.studentenwerk-karlsruhe.de/de/essen/?view=ok&STYLE=popup_plain&c=erzberger&p=1&kw=3',false,$context);

libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$nodes = $xpath->query("//table[@class='easy-tab-dot']");
//header("Content-type: text/plain");

foreach ($nodes as $i => $node) {
    $arr = array();

    $children = $node->childNodes;
    foreach ($children as $child) {
        $tmp_doc = new DOMDocument();
        $tmp_doc->appendChild($tmp_doc->importNode($child,true));       
        #echo $tmp_doc->saveHTML();
        print_r( $child );
    }
    echo "#######################################################################################";
}


文章来源: Parse HTML in PHP and return JSON