Javascript 如何通过无头 chrome 管理登录会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48608971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:11:20  来源:igfitidea点击:

how to manage log in session through headless chrome?

javascriptcookiesweb-scrapingheadlesspuppeteer

提问by Anton Kurtin

I need to make scraper to:

我需要使刮刀:

open headless browser, go to url, log in (there is steam oauth), fill some inputs, click 2 buttons

打开无头浏览器,转到url,登录(有 Steam oauth),填写一些输入,单击 2 个按钮

problem is every new instance of headless browser clears my login session, and then i need to login again and again...how to save it through instances? for example using puppeteer with headless chrome

问题是无头浏览器的每个新实例都会清除我的登录会话,然后我需要一次又一次地登录......如何通过实例保存它?例如使用 puppeteer 和 headless chrome

or how can i open already logged in chrome headless instance? if i already log in in my main chrome window

或者我怎样才能打开已经登录的 Chrome 无头实例?如果我已经登录我的主 chrome 窗口

采纳答案by Ramiro

In puppeter you have access to the session cookies through page.cookies().

在 puppeter 中,您可以通过page.cookies().

So once you log in, you could get every cookie and save it in a json file using jsonfile:

因此,一旦您登录,您就可以获取每个 cookie 并使用jsonfile将其保存在一个 json 文件中:

// Save Session Cookies
const cookiesObject = await page.cookies()
// Write cookies to temp file to be used in other profile pages
jsonfile.writeFile(cookiesFilePath, cookiesObject, { spaces: 2 },
 function(err) { 
  if (err) {
  console.log('The file could not be written.', err)
  }
  console.log('Session has been successfully saved')
})

Then, on your next iteration right before using page.goto()you can call page.setCookie()to load the cookies from the file one by one:

然后,在使用之前的下一次迭代中,page.goto()您可以调用page.setCookie()从文件中一一加载 cookie:

const previousSession = fileExistSync(cookiesFilePath)
if (previousSession) {
  // If file exist load the cookies
  const cookiesArr = require(`.${cookiesFilePath}`)
  if (cookiesArr.length !== 0) {
    for (let cookie of cookiesArr) {
      await page.setCookie(cookie)
    }
    console.log('Session has been loaded in the browser')
    return true
  }
}

Checkout the docs:

查看文档:

回答by meatherly

There is an option to save user data using the userDataDiroption when launching puppeteer. This stores the session and other things related to launching chrome.

userDataDir启动 puppeteer 时,可以使用该选项保存用户数据。这存储了与启动 chrome 相关的会话和其他内容。

puppeteer.launch({
  userDataDir: "./user_data"
});

It doesn't go into great detail but here's a link to the docs for it: https://pptr.dev/#?product=Puppeteer&version=v1.6.1&show=api-puppeteerlaunchoptions

它没有详细介绍,但这里有一个指向它的文档的链接:https: //pptr.dev/#?product=Puppeteer&version=v1.6.1&show=api-puppeteerlaunchoptions

回答by Daniel Porteous

For a version of the above solution that actually works and doesn't rely on jsonfile(instead using the more standard fs) check this out:

对于实际有效且不依赖jsonfile(而是使用更标准的fs)的上述解决方案的一个版本,请查看:

Setup:

设置:

const fs = require('fs');
const cookiesPath = "cookies.txt";

Reading the cookies (put this code first):

读取cookies(先放这段代码):

// If the cookies file exists, read the cookies.
const previousSession = fs.existsSync(cookiesPath)
if (previousSession) {
  const content = fs.readFileSync(cookiesPath);
  const cookiesArr = JSON.parse(content);
  if (cookiesArr.length !== 0) {
    for (let cookie of cookiesArr) {
      await page.setCookie(cookie)
    }
    console.log('Session has been loaded in the browser')
  }
}

Writing the cookies:

编写 cookie:

// Write Cookies
const cookiesObject = await page.cookies()
fs.writeFileSync(cookiesPath, JSON.stringify(cookiesObject));
console.log('Session has been saved to ' + cookiesPath);

回答by Meet Mahajan

For writing Cookies

用于写入 Cookie

async function writingCookies() {
const cookieArray = require(C.cookieFile); //C.cookieFile can be replaced by ('./filename.json')
await page.setCookie(...cookieArray);
await page.cookies(C.feedUrl); //C.url can be ('https://example.com')
}

For reading Cookies, for this, you've to install jsonfile in your project : npm install jsonfile

要读取 Cookie,为此,您必须在项目中安装 jsonfile: npm install jsonfile

async function getCookies() {
const cookiesObject = await page.cookies();
jsonfile.writeFile('linkedinCookies.json', cookiesObject, { spaces: 2 },
  function (err) {
    if (err) {
      console.log('The Cookie file could not be written.', err);
    }
    console.log("Cookie file has been successfully saved in current working Directory : '" + process.cwd() + "'");
  })
}

Call these two functions using awaitand it will work for you.

调用这两个函数使用await它会为你工作。