Javascript 如何通过Puppeter exposeFunction使用date.js?
我在这个例子中使用了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]; 返回{ 问题 时间 } }};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=> 日期文本; 等待页
你说它不工作是什么意思?我的意思是date函数在页面上下文之外工作得很好。但它在页面求值函数中不起作用。您需要在自己的页面中导入库scraping@Rashomon请你详细说明一下好吗?你说的主页是什么意思?对不起,我不知道你已经使用了exposeFunction。能否尝试使用window.formatDate访问formatDate?