@ #Node

Node.jsrequest 抓取页面时,页面经常会有检查用户权限等使用到 Cookie 的场景。实际上处理起来非常简单,直接指定参数 jar 即可。如下代码,代码会自动应用旧的 Cookie 并自动更新新的 Set-Cookie,然后接着抓取需要权限检查的页面就可以了。

代码示例:

const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.57';
const oldToken = 'ABCv8HGV9PBirrzA2QrL691t8ubdufpAqg:1697695874196';
const oldCookie = '__old_cookie__';
const req = require('request');
const cookieObj = req.jar();
const request = req.defaults({ jar: cookieObj, headers: {'User-Agent': userAgent, 'Cookie': oldCookie } });

function checkLoginState(){
	url = 'https://chrome.google.com/webstore/ajax/login-state?snuoi&hl=zh-CN&gl=HK&pv=20210820&mce=atf%2Cpii%2Crtr%2Crlb%2Cgtc%2Chcn%2Csvp%2Cwtd%2Chap%2Cnma%2Cdpb%2Cutb%2Chbh%2Cebo%2Chqb%2Cifm%2Cndd%2Cntd%2Coiw%2Cuga%2Cc3d%2Cncr%2Chns%2Cctm%2Cac%2Chot%2Chsf%2Chfi%2Cdtp%2Cmac%2Cbga%2Cpon%2Cfcf%2Crai%2Chbs%2Crma&_reqid=51075&rt=j';
	let data = { login: 'your@gmail.com', t: oldToken };
	request.post({url:url, form:data, json:0}, function (error, response, body0) {
		if(response.statusCode == 200){
		  log('Init Cookie: ' + cookieObj.getCookieString(url) + '\n');
		}
	});
}

文档信息

更多相关

手机 IMEI 码在线随机生成器

IMEI 是什么 IMEI(International Mobile Equipment Identity)国际移动设备识别码是手机的唯一标识符,用于识别区分全球范围内的移动设备。..