Extract data from website via PHP

后端 未结 6 530
难免孤独
难免孤独 2020-12-23 15:43

I am trying to create a simple alert app for some friends.

Basically i want to be able to extract data \"price\" and \"stock availability\" from a webpage like the f

6条回答
  •  死守一世寂寞
    2020-12-23 16:17

    It's called screen scraping, in case you need to google for it.

    I would suggest that you use a dom parser and xpath expressions instead. Feed the HTML through HtmlTidy first, to ensure that it's valid markup.

    For example:

    $html = file_get_contents("http://www.example.com");
    $html = tidy_repair_string($html);
    $doc = new DomDocument();
    $doc->loadHtml($html);
    $xpath = new DomXPath($doc);
    // Now query the document:
    foreach ($xpath->query('//table[@class="pricing"]/th') as $node) {
      echo $node, "\n";
    }
    

提交回复
热议问题