СЦЕНАРИЙ - У меня есть сценарий кукловода, который принимает URL-адрес и объект json в качестве аргументов для выполнения.- Он вызывается из php-скрипта из html-файла - скрипт puppeteer переходит на URL-адрес, извлекает содержимое страницы, console.log s, и, таким образом, контент становится доступным в html, содержащем вышеупомянутый php-скрипт.
ПРОБЛЕМА - на окнах он работает отлично, выдавая желаемый результат.Однако теперь я перенес свой проект на Ubuntu, и вот тут начинаются проблемы.- Контент не загружается, заканчивается пустой страницей.- Я запустил сценарий кукловода из консоли, и он отлично работает, выходит из страницы.НО, когда я вызываю его из скрипта php с помощью system (), это не так.НО он отлично работает на Windows даже из сценария php.
Вот мой код php
<?php
if(isset($_POST['url'])){
$split_url = str_replace('.', '_', explode('/', $_POST['url']));
$dir = "site_config/";
$content = '';
if( is_dir($dir) ){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
$filename = str_replace('.txt','',$file);
if($filename === $split_url[2]){
$content = file_get_contents($dir.$split_url[2].'.txt');
}
}
closedir($dh);
}
}
echo '<script>document.getElementById("website-input-form").style.display = "none";</script>';
/*to run with phantomjs*/
//system('phantomjs get_page_phantomjs.js "'.$_REQUEST['url'].'" '.$content);
/*to run with puppeteer*/
system('node get_page_puppeteer.js "'.$_REQUEST['url'].'" '.$content);
// system('node sample.js "'.$_REQUEST['url'].'" '.$content);
}
?>
Вы можете увидеть в последней строке, что я запускаю другой пример сценария nodejs.Он отлично выполнил.
Так что я не знаю, может быть, что-то не так со сценарием кукловода?
const puppeteer = require('puppeteer');
const url = process.argv[2]; //url from command line argument
const json = process.argv[3]; //config content from command line argument
/*_________________________STEP 1____________________________________*/
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle2',
timeout: 3000000
});
await page.evaluate(function(json){
//removing unwanted elements from html content
Array.prototype.slice.call(document.getElementsByTagName("script")).filter(function(script) {
return script.type != "application/ld+json";
}).forEach(function(script) {
script.parentNode.removeChild(script);
});
Array.prototype.slice.call(document.getElementsByTagName("style")).filter(function(style) {
return style.type != "application/ld+json";
}).forEach(function(style) {
style.parentNode.removeChild(style);
});
Array.prototype.slice.call(document.getElementsByTagName("iframe")).filter(function(iframe) {
return iframe.type != "application/ld+json";
}).forEach(function(iframe) {
iframe.parentNode.removeChild(iframe);
});
Array.prototype.slice.call(document.getElementsByTagName("video")).filter(function(video) {
return video.type != "application/ld+json";
}).forEach(function(video) {
video.parentNode.removeChild(video);
});
Array.prototype.slice.call(document.getElementsByTagName("img")).filter(function(img) {
img.setAttribute('style','max-width: 50% !important;');
return img.src.endsWith('.svg') === true;
}).forEach(function(img) {
img.parentNode.removeChild(img);
});
//providing the site's config through an element
var inp = document.createElement('div');
inp.setAttribute('textcontent', json);
inp.setAttribute('id', 'config_available');
var XMLS = new XMLSerializer();
var inp_xmls = XMLS.serializeToString(inp);
document.body.insertAdjacentHTML('afterbegin', inp_xmls);
//injecting the logic script
inp = document.createElement('script');
inp.setAttribute('src', './scraperJavascript.js');
inp.setAttribute('type', 'text/javascript');
XMLS = new XMLSerializer();
inp_xmls = XMLS.serializeToString(inp);
document.body.insertAdjacentHTML('afterbegin', inp_xmls);
}, json)
//rendering page's html
const renderedContent = await page.evaluate(() => new XMLSerializer().serializeToString(document));
console.log(renderedContent);
await browser.close();
}
run();
Но если со сценарием что-то не так, почему он успешно запускается из консоли (в Ubuntu и Windows) и из сценария php (в windows), но не из сценария php (в ubuntu)
UPDATE Я выполнил проверку исключений на конце кукловода.Исключение действительно происходит, и это его сообщение Error: Failed to launch chrome! [0608/095818.625603:ERROR:icu_util.cc(133)] Invalid file descriptor to ICU data received. [0608/095818.625662:FATAL:content_main_delegate.cc(57)] Check failed: false. #0 0x55dc5336182c base::debug::StackTrace::StackTrace() #1 0x55dc532e8290 logging::LogMessage::~LogMessage() #2 0x55dc51598de3 content::ContentMainDelegate::TerminateForFatalInitializationError() #3 0x55dc53017941 content::ContentMainRunnerImpl::Initialize() #4 0x55dc53021c12 service_manager::Main() #5 0x55dc53016184 content::ContentMain() #6 0x55dc571eea39 headless::(anonymous namespace)::RunContentMain() #7 0x55dc571eeac2 headless::HeadlessBrowserMain() #8 0x55dc5301ef8f headless::HeadlessShellMain() #9 0x55dc515971ac ChromeMain #10 0x7f5204329830 __libc_start_main #11 0x55dc5159702a _start TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md at onClose (/var/www/html/master/scraper_puppeteer/node_modules/puppeteer/lib/Launcher.js:255:14) at Interface.helper.addEventListener (/var/www/html/master/scraper_puppeteer/node_modules/puppeteer/lib/Launcher.js:244:50) at emitNone (events.js:111:20) at Interface.emit (events.js:208:7) at Interface.close (readline.js:370:8) at Socket.onend (readline.js:149:10) at emitNone (events.js:111:20) at Socket.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1055:12) at _combinedTickCallback (internal/process/next_tick.js:138:11)