Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过Puppeter exposeFunction使用date.js?_Javascript_Node.js_Web Scraping_Puppeteer - Fatal编程技术网

Javascript 如何通过Puppeter exposeFunction使用date.js?

Javascript 如何通过Puppeter exposeFunction使用date.js?,javascript,node.js,web-scraping,puppeteer,Javascript,Node.js,Web Scraping,Puppeteer,我在这个例子中使用了Puppeter 1.19.0和date.js 0.3.3 const puppeter=需要“puppeter”; const date=require'date.js'; 让scrape=async=>{ const browser=wait puppeter.launch{headless:false}; const page=await browser.newPage; 等待页面。exposeFunction'formatDate',text=> 日期文本; 等待页

我在这个例子中使用了Puppeter 1.19.0和date.js 0.3.3

const puppeter=需要“puppeter”; const date=require'date.js'; 让scrape=async=>{ const browser=wait puppeter.launch{headless:false}; const page=await browser.newPage; 等待页面。exposeFunction'formatDate',text=> 日期文本; 等待页面。转到'https://www.daraz.com.bd/products/the-vip-suction-mobile-phone-stand-pocket-size-i113492895-s1030756115.html'; 等待page.waitFor1000; const result=wait page.evaluate=>{ let elements=document.queryselectoral'module_product_qna>div.pdp-mod-qna>div:nth-child2>ul>li' 对于元素的var元素 { 让question=element.querySelector'div:first of type>div.qna-content'。innerText; 让qtime=element.querySelector'div:first of type>div.qna-meta'。innerText; 设q=qtime.match/-.+/; qtime=formatDateq[1]; 返回{ 问题 时间 } }}; browser.close; 返回结果; }; scrape.thenvalue=>{ console.logvalue; }; 来自木偶演员:

该方法在页面的窗口对象上添加一个名为name的函数。 调用时,该函数将执行node.js和 返回解析为的返回值的承诺 木偶功能

试试这个:

const result=wait page.evaluateasync=>{ let elements=document.queryselectoral'module_product_qna>div.pdp-mod-qna>div:nth-child2>ul>li' 对于元素的var元素 { 让question=element.querySelector'div:first of type>div.qna-content'。innerText; 让qtime=element.querySelector'div:first of type>div.qna-meta'。innerText; 设q=qtime.match/-.+/; qtime=wait window.formatDateq[1]; 返回{ 问题 时间 } }};
你说它不工作是什么意思?我的意思是date函数在页面上下文之外工作得很好。但它在页面求值函数中不起作用。您需要在自己的页面中导入库scraping@Rashomon请你详细说明一下好吗?你说的主页是什么意思?对不起,我不知道你已经使用了exposeFunction。能否尝试使用window.formatDate访问formatDate?