Created
March 25, 2018 10:51
-
-
Save SuperHuangXu/010eff614006aede867c44ec08460e5a to your computer and use it in GitHub Desktop.
使用 puppeteer 从教务处爬取个人信息
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as puppeteer from "puppeteer"; | |
import { Page } from "puppeteer"; | |
/** | |
* 登录 | |
* @param {string} username | |
* @param {string} password | |
* @returns {Promise<{code: number; data: {balance: string}; info: {}}>} | |
*/ | |
async function login(username: string, password: string) { | |
// 返回数据格式 | |
const result = { | |
code: 400, | |
data: { | |
balance: "", | |
info: {} | |
}, | |
}; | |
const WHXYURL: string = | |
"http://uia.whxy.edu.cn/cas/login?service=http%3A%2F%2Fportal.whxy.edu.cn%2Fc%2Fportal%2Flogin"; | |
const browser = await puppeteer.launch({ | |
// 设置Chromium位置 | |
executablePath: "D:\\Program Files (x86)\\Yarn\\chrome\\chrome.exe", | |
env: { | |
CHROME_PATH: puppeteer.executablePath() | |
} | |
// 是否显示浏览器 | |
// headless: false | |
// devtools: true | |
}); | |
const page = await browser.newPage(); | |
await page.goto(WHXYURL); | |
await page.type("#username", username); | |
await page.type("#password", password); | |
await page.click(".login"); | |
// 一卡通余额 | |
try { | |
await page.waitForSelector("#balance", { | |
timeout: 1000 | |
}); | |
result.data.balance = await page.$eval( | |
"#balance", | |
element => element.innerHTML | |
); | |
result.code = 200; | |
// 获取学生信息 | |
result.data.info = await getInfo(page); | |
return result; | |
} catch (e) { | |
return result; | |
} finally { | |
await browser.close(); | |
} | |
} | |
/** | |
* 获取学生信息 | |
* @param {Page} page | |
* @returns {Promise<{name: string; sex: string; xy: string; zy: string; bj: string}>} | |
*/ | |
async function getInfo(page: Page) { | |
const info = { | |
name: "", | |
sex: "", | |
// 学院 | |
xy: "", | |
// 专业 | |
zy: "", | |
// 班级 | |
bj: "" | |
}; | |
// 教务系统 url | |
const jwxt = | |
"http://portal.whxy.edu.cn/web/guest/appcenter?p_p_id=jigsawAppFront_WAR_jigsawAppFrontportlet&p_p_lifecycle=0&p_p_state=pop_up&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_jigsawAppFront_WAR_jigsawAppFrontportlet_action=linkurl&_jigsawAppFront_WAR_jigsawAppFrontportlet_appid=125708"; | |
try { | |
await page.goto(jwxt); | |
// 出现重复登录,踢出其它登录 | |
await page.reload(); | |
await page.waitForSelector(".acitem", { | |
timeout: 1000 | |
}); | |
// 学籍信息 | |
const stdDetail = "http://59.172.226.5/eams/stdDetail.action"; | |
await page.goto(stdDetail); | |
info.name = await page.$eval( | |
"#studentInfoTb tr:nth-child(2) td:nth-child(4)", | |
elements => elements.textContent | |
); | |
info.sex = await page.$eval( | |
"#studentInfoTb tr:nth-child(3) td:nth-child(4)", | |
element => element.textContent | |
); | |
info.zy = await page.$eval( | |
"#studentInfoTb tr:nth-child(7) td:nth-child(2)", | |
element => element.textContent | |
); | |
info.xy = await page.$eval( | |
"#studentInfoTb tr:nth-child(10) td:nth-child(2)", | |
element => element.textContent | |
); | |
info.bj = await page.$eval( | |
"#studentInfoTb tr:nth-child(12) td:nth-child(4)", | |
element => element.textContent | |
); | |
} catch (e) { | |
console.log(e); | |
} | |
return info; | |
} | |
login("2015*****", "Whxy27****").then(value => { | |
console.log(value); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment