Извлечение правильного значения из веб-страницы с Goutte - PullRequest
0 голосов
/ 26 марта 2019

Я установил Goutte в свое приложение Laravel 5.7 и пытаюсь собрать значения для COAL, GAS, HYDRO и WING (столбец TNG) со страницы:

http://ets.aeso.ca/ets_web/ip/Market/Reports/CSDReportServlet

Route::get('hdtuto', function () {
    $crawler = Goutte::request('GET', 'http://ets.aeso.ca/ets_web/ip/Market/Reports/CSDReportServlet');
    $aeso_data = $crawler->filter('TABLE > TR > TD');
    dd($aeso_data);
});

Я надеялся, что мне удастся выполнить обход узлов с помощью этой опции:

$crawler->filter('body > p')->eq(0);

Согласно этому руководству:

https://symfony.com/doc/current/components/dom_crawler.html?any#node-traversing

Чтобы я мог со временем сделать что-то вроде этого:

$coal = $crawler->filter('TABLE > TR > TD')->eq(15);
$gas = $crawler->filter('TABLE > TR > TD')->eq(20);
$hydro = $crawler->filter('TABLE > TR > TD')->eq(25);
$wind = $crawler->filter('TABLE > TR > TD')->eq(30);

Вот пример того, что я сейчас получаю:

Crawler {#471 ▼
  #uri: "http://ets.aeso.ca/ets_web/ip/Market/Reports/CSDReportServlet"
  -defaultNamespacePrefix: "default"
  -namespaces: []
  -baseHref: "http://ets.aeso.ca/ets_web/ip/Market/Reports/CSDReportServlet"
  -document: DOMDocument {#407 ▶}
  -nodes: array:598 [▼
    0 => DOMElement {#469 ▼
      +nodeName: "td"
      +nodeValue: DOMImplementation {#430 ▶}
      +nodeType: DOMDocumentType {#443 …}
      +parentNode: DOMElement {#422}
      +childNodes: DOMNodeList {#441 …1}
      +firstChild: DOMElement {#442}
      +lastChild: DOMElement {#442}
      +previousSibling: DOMNodeList {#432 ▶}
      +nextSibling: DOMText {#451}
      +attributes: DOMNamedNodeMap {#452 …1}
      +ownerDocument: DOMDocument {#407 ▶}
      +namespaceURI: null
      +prefix: ""
      +localName: "td"
      +baseURI: null
      +textContent: ""
      +tagName: "td"
      +schemaTypeInfo: null
    }
    1 => DOMElement {#468 ▼
      +nodeName: "td"
      +nodeValue: ""
      +nodeType: XML_ELEMENT_NODE
      +parentNode: DOMElement {#1070}
      +childNodes: DOMNodeList {#1071 …1}
      +firstChild: DOMText {#1073}
      +lastChild: DOMText {#1073}
      +previousSibling: null
      +nextSibling: null
      +attributes: DOMNamedNodeMap {#1077 …1}
      +ownerDocument: DOMDocument {#407 ▶}
      +namespaceURI: null
      +prefix: ""
      +localName: "td"
      +baseURI: null
      +textContent: ""
      +tagName: "td"
      +schemaTypeInfo: null

1 Ответ

0 голосов
/ 28 марта 2019

Я использовал это:

Route::get('scrapertest', function() {

    $crawler = Goutte::request('GET', 'http://ets.aeso.ca/ets_web/ip/Market/Reports/CSDReportServlet');
    $crawler2 = Goutte::request('GET', 'http://ets.aeso.ca/ets_web/ip/Market/Reports/DailyAveragePoolPriceReportServlet');

    $values = $crawler->filter('tr > td')->each(function ($node) {
        return $node->text();
    });
// dd($values);

    $values2 = $crawler2->filter('tr > td')->each(function ($node) {
        return $node->text();
    });
// dd($values2);
    $total = $values[11];
    $internal_load = $values[15];
    $net_to_grid = $values[17];
    $coal = $values[36];
    $gas =  $values[40];
    $hydro = $values[44];
    $wind = $values[52];

    echo 'Scraper test <br>';
    echo 'Alberta Total Net Generation '.$total.'<br>';
    echo 'Alberta Internal Load  '.$internal_load.'<br>';
    echo 'Net-To-Grid Generation '.$net_to_grid.'<br>';
    echo 'Coal '.$coal.'<br>';
    echo 'Gas '.$gas.'<br>';
    echo 'Hydro '.$hydro.'<br>';
    echo 'Wind '.$wind.'<br>';
});
...