
const puppeteer = require('puppeteer');
void(async () => { const browser = await puppeteer.launch({ headless: false, devtools: true }); const page = await browser.newPage(); const UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3835.0 Safari/537.36"; // UserAgent ; await Promise.all ( [ page.setUserAgent ( UA ) , page.setJavascriptEnabled ( true ) , // 允许执行 js 脚本 ] ) ; await page.goto( "http://www.gewara.com/" , { waitUntil : "networkidle0" , timeout : 3600000 } ) ; await page .mainFrame() .addScriptTag({ url: 'https://code.jquery.com/jquery-3.4.1.min.js' }); await page.waitFor(2000); page.on( 'console' , msg => console.log( "var result = await page.evaluate( ( ) =>{ *** 函数内的 console.log:" , msg.text() ) ); const result = await page.evaluate(() => { const watchDog = ( window.$ !== undefined ); console.log( "window.$ !== undefined JQ:" + watchDog ) ; // 应该显示 true ;实际测试却显示 false ;但是,浏览器运行打开网页后,在控制面板测试,就显示为 true 了 ; return }); console.log(result); })(); 1 hgjian OP console.log( "window.$ !== undefined JQ:" + watchDog ) ; // 应该显示 true ;实际测试却显示 false ;但是,浏览器运行打开网页后,在控制面板测试,就显示为 true 了 ; |
2 2DaYe 2019-07-18 12:23:40 +08:00 jQuery 库引用晚了吧?页面都 load 完了再引,jQuery 就没法初始化了吧? |
4 /div> learnshare 2019-07-18 12:39:43 +08:00 const page = await browser.newPage(); await page.addScriptTag({ path: './node_modules/jquery/dist/jquery.js', }); await page.goto() |
5 hgjian OP @learnshare 安装 jq 库? |
6 hgjian OP @learnshare 你好,我按照你的代码试了一下,还是不行哦 |
7 learnshare 2019-07-18 16:00:10 +08:00 @hgjian 大概你测试的网站不允许 jQuery,测试本站是可以的 https://gist.github.com/LearnShare/2af03a1d6bcb41a9ec81b51e25435e3d |
8 hgjian OP @learnshare 应该是可以的吧,puppeteer 打开 chrome 浏览器运行网页后,jquery 就生效了 ; |
9 learnshare 2019-07-18 16:13:50 +08:00 @hgjian 但实际测下来就是没有 $ |
10 hgjian OP @learnshare 那好奇怪啊,在浏览器控制台执行 window.$(".city-current").innerText 可以取得结果 |
11 hgjian OP 我中间有成功过一次,后来改乱了,不知道成功的代码是什么样的了, |
12 Melting 2019-07-18 17:17:20 +08:00 好像是因为 http 的关系,而且话说 chrome 命令行也有$这个命令,可能冲突了 |
15 shinpei 2019-07-18 20:22:38 +08:00 这个就涉及到 jQuery 源码的问题了; jquery 在代码尾部全局注册: if ( !noGlobal ) { window.jQuery = window.$ = jQuery; } // 这个是开始的代码,由于这个网站 module 是存在的,所以 noGlobal 为 true, ( function( global, factory ) { "use strict"; if ( typeof module === "object" && typeof module.exports === "object" ) { // For CommonJS and CommonJS-like environments where a proper `window` // is present, execute the factory and get jQuery. // For environments that do not have a `window` with a `document` // (such as Node.js), expose a factory as module.exports. // This accentuates the need for the creation of a real `window`. // e.g. var jQuery = require("jquery")(window); // See ticket #14549 for more info. module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); }; } else { factory( global ); } // Pass this if window is not defined yet } )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {...} |
16 shinpei 2019-07-18 20:26:35 +08:00 简单点就是你去修改下 jquery 源码,把 if (!noGlobal) 判断去掉 |
21 shinpei 2019-07-18 21:03:17 +08:00 @hgjian 我的意思是把 if ( !noGlobal ) { window.jQuery = window.$ = jQuery; } 直接写成 window.jQuery = window.$ = jQuery; |
23 hgjian OP @shinpei 搞定了, await page.addScriptTag({ path: './node_modules/jquery/dist/jquery.js', }); 应该放在 page.goto 的后面,就可以了 谢谢啊 |
25 123s 2019-07-19 14:34:25 +08:00 根本不需要 jq 吧 |
26 xuyl 2019-07-26 14:22:19 +08:00 cheerio 就是服务端的 jQuery |