在 nodejs cookie 上解析站点时如何记住授权
How to remember authorization when parsing a site on nodejs cookies
我去网站寻找授权表,如果我找到了,我就登录,如果没有,我就做我需要的操作
let cookie_ = fs.readFileSync("cookies.json");//I am looking for a file with saved cookies
cookie = JSON.parse(cookie_);//Converting to json
nightmare
.goto('https://site.ru/login')//I go to the site
.cookies.set(cookie)//I substitute cookies from the file
.evaluate(function () {
return document.querySelector('input[id="email"]');//I am looking for a field to enter mail
})
.then(function (page) {
if(page) {//I check if there is a field for entering mail
f().then(function (cookies) {//We get the result from the function
require('fs').writeFileSync(//And write to file
'cookies.json',
JSON.stringify(cookies)
);
})
} else {
console.log('You are logged in');
}
})
async function f() {//I call the function if we are not authorized
return new Promise((resolve, reject) => {
nightmare
.goto('https://site.ru/login')
.type('input[id="email"]', 'login')//Enter mail
.type('input[id="password"]', 'passord')//Enter your password
.click('.btn.btn-danger')//Click on the authorization button
.wait(2000)//We wait 2 seconds
.cookies.get()//We receive cookies
.then(resolve)
});
}
文件已创建,cookie 已写入,但在下一次尝试 运行 脚本时,授权表单仍然出现
我也试过先去-goto('about: blank')
然后设置cookies
然后去goto('https://site.ru/login')
错误 - UnhandledPromiseRejectionWarning: Error: Setting cookie failed
很遗憾,无法通过nightmare
解决问题
用 puppeteer
解决
示例 - 将 cookie 保存到文件
const puppeteer = require('puppeteer')
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://github.com/login')
await page.type('#login_field', process.env.GITHUB_USER)
await page.type('#password', process.env.GITHUB_PWD)
await page.waitForSelector('.js-cookie-consent-reject')
await page.click('.js-cookie-consent-reject')
await page.$eval('[name="commit"]', (elem) => elem.click())
await page.waitForNavigation()
const cookies = await page.cookies()
const cookieJson = JSON.stringify(cookies)
fs.writeFileSync('cookies.json', cookieJson)
await browser.close()
})()
从文件中读取 cookies
const puppeteer = require('puppeteer')
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const cookies = fs.readFileSync('cookies.json', 'utf8')
const deserializedCookies = JSON.parse(cookies)
await page.setCookie(...deserializedCookies)
await page.goto(`https://github.com/${process.env.GITHUB_USER}`)
await browser.close()
})()
我去网站寻找授权表,如果我找到了,我就登录,如果没有,我就做我需要的操作
let cookie_ = fs.readFileSync("cookies.json");//I am looking for a file with saved cookies
cookie = JSON.parse(cookie_);//Converting to json
nightmare
.goto('https://site.ru/login')//I go to the site
.cookies.set(cookie)//I substitute cookies from the file
.evaluate(function () {
return document.querySelector('input[id="email"]');//I am looking for a field to enter mail
})
.then(function (page) {
if(page) {//I check if there is a field for entering mail
f().then(function (cookies) {//We get the result from the function
require('fs').writeFileSync(//And write to file
'cookies.json',
JSON.stringify(cookies)
);
})
} else {
console.log('You are logged in');
}
})
async function f() {//I call the function if we are not authorized
return new Promise((resolve, reject) => {
nightmare
.goto('https://site.ru/login')
.type('input[id="email"]', 'login')//Enter mail
.type('input[id="password"]', 'passord')//Enter your password
.click('.btn.btn-danger')//Click on the authorization button
.wait(2000)//We wait 2 seconds
.cookies.get()//We receive cookies
.then(resolve)
});
}
文件已创建,cookie 已写入,但在下一次尝试 运行 脚本时,授权表单仍然出现
我也试过先去-goto('about: blank')
然后设置cookies
然后去goto('https://site.ru/login')
错误 - UnhandledPromiseRejectionWarning: Error: Setting cookie failed
很遗憾,无法通过nightmare
解决问题
用 puppeteer
示例 - 将 cookie 保存到文件
const puppeteer = require('puppeteer')
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://github.com/login')
await page.type('#login_field', process.env.GITHUB_USER)
await page.type('#password', process.env.GITHUB_PWD)
await page.waitForSelector('.js-cookie-consent-reject')
await page.click('.js-cookie-consent-reject')
await page.$eval('[name="commit"]', (elem) => elem.click())
await page.waitForNavigation()
const cookies = await page.cookies()
const cookieJson = JSON.stringify(cookies)
fs.writeFileSync('cookies.json', cookieJson)
await browser.close()
})()
从文件中读取 cookies
const puppeteer = require('puppeteer')
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
const cookies = fs.readFileSync('cookies.json', 'utf8')
const deserializedCookies = JSON.parse(cookies)
await page.setCookie(...deserializedCookies)
await page.goto(`https://github.com/${process.env.GITHUB_USER}`)
await browser.close()
})()