I am writing a php where one of the function is to scrape data from internet by Puppeteer. Thank you ggorlen for his help, my js work porperly. Now, I want to run the node.js in my php. I searched in the internet and try to imitate some examples but it fails. Here is my php(Bulletin Translator.php):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>contacts.php</title>
</head>
<body text="blue">
<?php
exec('cd js');
exec('node index.js');
?>
<?php
// Some php code here.
?>
</body>
</html>
The scraping js is put inside the js folder and is shown below: Structure1 Structure2
index.js:
const puppeteer = require('puppeteer');
//var date_in_YMD = new Date();
(async ()=>
{
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(weather_report_chin_html)
// let's just call them tweetHandle
//const bulletin_urls = await page.$$('div.leftBody > ul[class^="list"]');
const bulletin_urls = await page.$$('div.leftBody');
// loop thru all handles
for(const bulletin_url of bulletin_urls)
{
try
{
const data = await page.$$eval(".NEW", els => els.map(el => (
{
text: el.textContent,
href: el.href,
})));
console.log(data);
}
catch(err)
{
console.error(err);
}
}
await browser.close()
}) ();
What I shall I do to run node.js in my php? And how shall I import my scraping result into my php? Any suggestion will be appreciated.
execruns whatever you pass it in a new child process, and once complete the process is destroyed. This means that you can’t chain multiple calls as you are doing