JavaScript — как я могу использовать simple_html_dom с phantomjs

Я пытаюсь заставить эти две библиотеки работать друг с другом, мой текущий код выглядит так:

phantomjs.js

var page = require('webpage').create();
var system = require('system');
var address = system.args[1];
page.open(address, function () {
var content = page.content;
console.log(content);
phantom.exit();
});

scraper.php

exec('phantomjs assets/phantomjs.js '.$page, $output);
$html2 = str_get_html($output);

Что я получаю обратно это:

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 91

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1139

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1149

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Warning: preg_match_all() expects parameter 2 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1620

Warning: strlen() expects parameter 1 to be string, array given in D:\XAMPP\htdocs\assets\php\simple_html_dom.php on line 1632

Функция simple_html_dom, которая возвращает ошибку:

// remove noise from html content
// save the noise in the $this->noise array.
protected function remove_noise($pattern, $remove_tag=false)
{
global $debugObject;
if (is_object($debugObject)) { $debugObject->debugLogEntry(1); }

$count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);

for ($i=$count-1; $i>-1; --$i)
{
$key = '___noise___'.sprintf('% 5d', count($this->noise)+1000);
if (is_object($debugObject)) { $debugObject->debugLog(2, 'key is: ' . $key); }
$idx = ($remove_tag) ? 0 : 1;
$this->noise[$key] = $matches[$i][$idx][0];
$this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));
}

// reset the length of content
$this->size = strlen($this->doc);
if ($this->size>0)
{
$this->char = $this->doc[0];
}
}

Когда я использую var_dump($output) Я получил html сайта, поэтому я знаю, что команда работает, но simple_html_dom, похоже, не принимает ее!

0

Решение

Проблема заключается в следующем: $output это массив, но str_get_html ожидает строку в качестве аргумента. Поэтому убедитесь, что вы конвертируете $ output в строку перед ее анализом.

1

Другие решения

Что-то вроде:

$body = `phantomjs myscript.js`;
$doc = str_get_html($body);

Вам может понадобиться setTimeout в скрипте phantomjs, у вас не так много времени для загрузки dom.

0

По вопросам рекламы [email protected]