Skip to content

Instantly share code, notes, and snippets.

@raphaelsoul
Created February 4, 2021 14:25
Show Gist options
  • Select an option

  • Save raphaelsoul/c94e01734696fdbc586ecd2df907e603 to your computer and use it in GitHub Desktop.

Select an option

Save raphaelsoul/c94e01734696fdbc586ecd2df907e603 to your computer and use it in GitHub Desktop.
QQMusicLogin
const login = async () => {
const browser = await puppeteer.launch({
headless: false,
devtools: true,
});
try {
const page = await browser.newPage();
await page.goto('https://y.qq.com', {
waitUntil: 'networkidle0'
});
await page.waitForSelector('#divdialog_0 > div.popup__hd > a > i.popup__icon_close', { visible: true, timeout: 3000 })
try {
await page.click('#divdialog_0 > div.popup__hd > a > i.popup__icon_close')
} catch (e) {
console.error(e)
}
// 点击导航登录
await page.click('body > div.mod_header > div > div.header__opt > span > a.top_login__link.js_login')
const $frame_tips = await page.waitForSelector('#frame_tips')
const frame_tips = await $frame_tips.contentFrame()
const $ptlogin_iframe = await frame_tips.waitForSelector('#ptlogin_iframe')
const ptlogin_iframe = await $ptlogin_iframe.contentFrame()
await ptlogin_iframe.waitForTimeout(3000)
// 切换账户/密码登录方式
await ptlogin_iframe.click('#switcher_plogin')
// 等待表单展现
await Promise.all([
ptlogin_iframe.waitForSelector('#u', { visible: true }),
ptlogin_iframe.waitForSelector('#p', { visible: true }),
ptlogin_iframe.waitForSelector('#login_button', { visible: true }),
])
// 登录
await ptlogin_iframe.type('#u', 'QQ号', { delay: 100 });
await ptlogin_iframe.type('#p', '密码', { delay: 100 });
await ptlogin_iframe.click('#login_button', { delay: 100 });
// 等待登录完成
await page.waitForSelector('body > div.mod_header > div > div.header__opt > span > a.top_login__link.js_logined')
// const frame_tips = (await page.$('#frame_tips')).contentFrame()
// const ptlogin_iframe = (await frame_tips.$('#ptlogin_iframe')).contentFrame()
// const $ptloginFrame = frame.contentFrame()
// await $ptloginFrame.waitForTimeout(3000)
await browser.close()
return await page.cookies()
} catch (e) {
await browser.close()
throw e
}
}
// cookies you got like this:
var cookies = [
{
"name": "ptui_loginuin",
"value": "[FILTER]",
"domain": ".qq.com",
"path": "/",
"expires": 1615039722,
"size": 22,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false
},
{
"name": "userAction",
"value": "1",
"domain": ".y.qq.com",
"path": "/",
"expires": 1612534115,
"size": 11,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false
},
{
"name": "pgv_pvid",
"value": "[FILTER]",
"domain": ".qq.com",
"path": "/",
"expires": 2147385600,
"size": 17,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false
},
{
"name": "ts_last",
"value": "y.qq.com/",
"domain": ".y.qq.com",
"path": "/",
"expires": 1612449511,
"size": 16,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false
},
{
"name": "pgv_info",
"value": "ssid=[FILTER]",
"domain": ".qq.com",
"path": "/",
"expires": -1,
"size": 24,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false
},
{
"name": "ts_uid",
"value": "[FILTER]",
"domain": ".y.qq.com",
"path": "/",
"expires": 1675519711,
"size": 16,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false
},
{
"name": "yqq_stat",
"value": "0",
"domain": ".y.qq.com",
"path": "/",
"expires": -1,
"size": 9,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment