使用JavaScript代码从Laravel Controller运行Puppeteer

问题描述 投票:0回答:2

当我尝试在节点shell中仅运行代码的这一部分时:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('http://my-url.com'); const button = await page.$('button#button'); await button.evaluate( button => button.click() ); await page.waitForTimeout(5000); ... await browser.close(); })();

它的工作正常,我还可以看到打开浏览器。当我尝试运行整个功能时,它不起作用。因此,我假定我在控制器中运行JS代码的方式是错误的。
	

I为JavaScript代码创建了一个新的JS文件

script.js

// script.js const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('http://my-url.com'); const button = await page.$('button#button'); await button.evaluate( button => button.click() ); await page.waitForTimeout(5000); ... await browser.close(); })();

javascript php laravel controller puppeteer
2个回答
0
投票

$process = new Process(['path/to/node', 'path/to/script.js]); $process->run(); if(!$process->isSuccessful()) { throw new ProcessFailedException($process); } $output = $process->getOutput(); $errors = $process->getErrorOutput();

为我工作


我想分享我的解决方案,以解决类似问题。我花了几天的时间试图解决它。我的刮板脚本在终端上有效,但是当我从控制器打电话时没有。因此,最后,这就是我需要配置控制器以使其正常工作的方式。这是代码;我希望它对某人有帮助。

运气好!
public function scrape(Request $request) {
    $data = $request->json()->all();
    $url = $data['siteUrl'] ?? null;

    $nodePath = "C:\\Program Files\\nodejs\\node.exe";
    $scriptPath = base_path('scraper.js');
    
    // Postavljanje radnog direktorija (ako je potrebno)
    chdir(base_path());  // Postavi radni direktorij na root Laravel aplikacije

    $command = "\"$nodePath\" \"$scriptPath\" \"$url\"";

    // Pokretanje komande
    exec($command, $output, $status);

    // Logiranje izlaza
    if ($status !== 0) {
        \Log::error("Node.js skripta nije uspjela: " . implode("\n", $output));
    } else {
        \Log::info("Izlaz iz Node.js skripte: " . implode("\n", $output));
    }

    return response()->json(['html' => $output]);
}

Scraper.js

0
投票
const puppeteer = require("puppeteer"); const url = process.argv[2]; async function scrape() { try { const browser = await puppeteer.launch({ headless: false , //probaj vidjeti neki delay dok se učita // headless: false, //vrati više podatak, pazi ignoreDefaultArgs: ['--disable-extensions'], args: [ '--disable-gpu', '--disable-setuid-sandbox', '--no-sandbox', '--no-zygote', '--disable-extensions' , "--disable-notifications", "--disable-geolocation"], }); const page = await browser.newPage(); await page.goto(url, { waitUntil: "domcontentloaded" }); const content = await page.content(); console.log(content); await browser.close(); return content; } catch (error) { console.error("Greška:", error); process.exit(1); } } scrape();

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.