# JS执行方法示例 1. 使用无头浏览器`Pyppeteer` : 使用`page.evaluate()`方法执行JS 2. 使用`PyexecJS`执行JS: 通过`execjs.compile()`编译JS语句返回`context`,然后通过`context.eval()`方法获取参数信息 我们以漫画网为例子: ## 分析过程 1. 打开单个章节地址: https://www.kanman.com/25934/dpcq_1h.html 2. 打开开发者模式(F12) : 之后刷新以下, 点击`Network`中的页面,查看`Response`应答页面原始内容,可以找到当前章节的所有参数内容 3. 通过正则表达式提取出JS内容,执行JS内容获取参数信息。 ## 方法1:通过`PyexecJS`库执行JS ```python import requests import execjs import re resp = requests.get('https://www.kanman.com/25934/dpcq_1h.html') res = re.findall(r'<script>.*?window\.(comicInfo.*?)</script>', resp.text) js_var = 'var ' + res[0] print(js_var) ``` var comicInfo={comic_id:25934,comic_newid:"doupocangqiong",comic_name:"斗破苍穹",last_chapter_id:"1743153",last_chapter_newid:"di864hua-1599272930012",last_chapter_name:"第864话 机关阵",show_type:1,readtype:1,comic_status:1,charge_paid:0,charge_coin_free:1,update_time:1599278405e3,boo_virtual_coin:!0,charge_status:"10000000000000000000",charge_share_free:1,charge_advertise_free:1,charge_truetime_free:1,charge_limittime_free:1,charge_limitline_free:1,charge_vip_free:1,charge_spread_free:1,charge_game_free:1,charge_coupons_free:1,charge_lottery_free:1,charge_limittime_paid:1,charge_limitline_paid:1,charge_others_paid:1,charge_credit_paid:1,is_copyright:1,current_chapter:{chapter_name:"第1话 陨落的天才(上)",chapter_newid:"dpcq_1h",chapter_id:1400558,chapter_domain_suffix:"",chapter_domain:"jumanhua.com",start_num:1,end_num:13,price:0,chapter_image_addr:"",create_date:1502988183537,rule:"/comic/D/斗破苍穹拆分版/1话/$$.jpg"},prev_chapter:null,next_chapter:{chapter_id:1400559,chapter_newid:"dpcq_2h",chapter_name:"第2话 陨落的天才(中)",price:0,chapter_image_addr:"",create_time:"2020-07-01T09:11:05.000Z",topic_copyright:"",chapter_domain:"jumanhua.com",rule:"/comic/D/斗破苍穹拆分版/2话/$$.jpg",start_num:1,end_num:12,create_date:1502988183537,chapter_domain_suffix:""},seoTitleTemplate:{default:"{0}{1} {0}漫画{1}",title:"{0}{1}话 {2} {0}漫画{1}话 {2} 看漫画"}} ```python ctx = execjs.compile(js_var) current_chapter = ctx.eval('comicInfo.current_chapter') current_chapter ``` {'chapter_name': '第1话 陨落的天才(上)', 'chapter_newid': 'dpcq_1h', 'chapter_id': 1400558, 'chapter_domain_suffix': '', 'chapter_domain': 'jumanhua.com', 'start_num': 1, 'end_num': 13, 'price': 0, 'chapter_image_addr': '', 'create_date': 1502988183537, 'rule': '/comic/D/斗破苍穹拆分版/1话/$$.jpg'} ## 方法2: 通过`pyppeteer`方法执行JS ```python from pyppeteer import launch import asyncio ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' proxy_server = "--proxy-server=socks5://127.0.0.1:1084" browser = await launch(headless=False, args=['--disable-infobars', proxy_server]) pages = await browser.pages() page1 = pages[0] await page1.setViewport(viewport={'width': 1280, 'height': 800}) await page1.setUserAgent(ua) ``` ```python await page1.goto('https://www.kanman.com/25934/dpcq_1h.html') ``` <pyppeteer.network_manager.Response at 0x7fbd63769400> ```python js_get = r'() =>{ return window.comicInfo.current_chapter; }' text_js1 = await page1.evaluate(js_get) text_js1 ``` {'chapter_name': '第1话 陨落的天才(上)', 'chapter_newid': 'dpcq_1h', 'chapter_id': 1400558, 'chapter_domain_suffix': '', 'chapter_domain': 'jumanhua.com', 'start_num': 1, 'end_num': 13, 'price': 0, 'chapter_image_addr': '', 'create_date': 1502988183537, 'rule': '/comic/D/斗破苍穹拆分版/1话/$$.jpg'} ```python ch_name = text_js1['chapter_name'] chapter_id = text_js1['chapter_id'] start_num = text_js1['start_num'] end_num = text_js1['end_num'] rule = text_js1['rule'] prefix = 'https://mhpic.jumanhua.com' suffix = '-kmh.middle.webp' for i in range(start_num, end_num+1): img_url = prefix + rule.replace(r'$$', str(i)) + suffix print(ch_name, img_url) ``` 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/1.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/2.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/3.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/4.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/5.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/6.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/7.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/8.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/9.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/10.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/11.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/12.jpg-kmh.middle.webp 第1话 陨落的天才(上) https://mhpic.jumanhua.com/comic/D/斗破苍穹拆分版/1话/13.jpg-kmh.middle.webp ```python browser.close() ```