Parse HTML in PHP and return JSON

2019-08-04 04:06发布

I am using PHP Simple HTML DOM Parser in my PHP script to parse information from a website into a JSON object. My JSON object should be formatted like this in the end:

Array with maximum 5 objects (Monday to Friday) or less (Tuesday–Friday etc).

All of these objects should have two arrays, one called food1 and one called food 2. Both of these arrays should contain multiple food names and their prices. I think in JSON it would look like this:

    {
  "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"
        }
      ]
    }
  ]
}

Anyway I previously only worked with Objective-C and having problems with solving this problem in PHP. I have also implemented a parser in Objective-C that works, but if the site changes their structure I would have to re-submit the whole app etc. That’s why I wanted to make a web service where I can dynamically change the parser outside of the app. All I got is this:

<?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;

?>

Which gives me all the food names but it isn’t sorted for the days and also not for the different food menus (there are two different menus on each day which are called food1 and food2 in my example JSON object).

In my Objective-C parser I just created a new day object when the food name is “SchniPoSa” and added all the following food names to food1 until there comes the food name “Salatbuffet” that and all the following food names I added to food2 array until there comes the next “SchniPoSa” food name. But this isn’t very good because the structure could change every day.

Also, I do not even know how to implement that in PHP. In my little PHP script I also don’t parse all the prices which are in the tag <span class="bgp price_1">.

Here is the website from which I want to parse the information:

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

Is there anyone who can help me with parsing the information in a valid JSON object like I described below?

1条回答
戒情不戒烟
2楼-- · 2019-08-04 04:49

Just saw you message and realised I hadn't gotten back to you about this. Maybe this will lead you in the right direction:

<?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 "#######################################################################################";
}
查看更多
登录 后发表回答