FriendsOfPHP / Goutte

Goutte, a simple PHP Web Scraper
MIT License
9.26k stars 1.01k forks source link

Goutte - Get table column #317

Open tiger899 opened 6 years ago

tiger899 commented 6 years ago

I have this code:

require ('vendor/autoload.php');

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;

$goutteClient = new Client();
$guzzleClient = new GuzzleClient(array(
    'timeout' => 6000,
));
$goutteClient->setClient($guzzleClient);

  $crawler = $goutteClient->request('GET', http://www.cophieu68.vn/stockonline_node.php?stcid=1);

  // Get the latest post in this category and display the titles
  $news = $crawler->filter('table#board_online>tbody>tr')->nextAll()->nextAll()->each(function ($node) {
    //  print_r($node->text());         
      return [              

          'v1' => $node->filter('td')->eq(0)->text(),   
          'v2' => $node->filter('td')->eq(1)->text(),   
          'v3' => $node->filter('td')->eq(2)->text(),                               
          'v12' => $node->filter('td')->eq(3)->text(),
          'v14' => $node->filter('td')->eq(4)->text(),
          'v16' => $node->filter('td')->eq(5)->text(),
          'v12' => $node->filter('td')->eq(6)->text(),
          'v13' => $node->filter('td')->eq(7)->text(),
          'v15' => $node->filter('td')->eq(8)->text(),
          'v16' => $node->filter('td')->eq(9)->text(),
          'v17' => $node->filter('td')->eq(10)->text(),
          'v18' => $node->filter('td')->eq(11)->text(),

      ];
  });

    print_r($news);

It show message:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'The current node list is empty.' in E:\xampp\htdocs\cr\vendor\symfony\dom-crawler\Crawler.php:568 Stack trace: #0 E:\xampp\htdocs\cr\data_now.php(43): Symfony\Component\DomCrawler\Crawler->text() #1 E:\xampp\htdocs\cr\vendor\symfony\dom-crawler\Crawler.php(371): {closure}(Object(Symfony\Component\DomCrawler\Crawler), 345) #2 E:\xampp\htdocs\cr\data_now.php(61): Symfony\Component\DomCrawler\Crawler->each(Object(Closure)) #3 E:\xampp\htdocs\cr\data_now.php(105): doCraw_old('http://www.coph...', 1, '2017-09-19') #4 {main} thrown in E:\xampp\htdocs\cr\vendor\symfony\dom-crawler\Crawler.php on line 568

I want it return array, but it error, please help me !

kevinjon27 commented 6 years ago

try this @tiger899

$news = $crawler->filterXPath('//table[@id="board_online"]')->filter('tr')->each(function ($tr, $i) { return $tr->filter('td')->each(function ($td, $i) { return trim($td->text()); }); });

tiger899 commented 6 years ago

Many Thanks !