import execjs import requests from lxml import etree def getRsa(u, p, lt): """ 加密方法 :param u:账号 :param p:密码 :param lt:lt :return:加密值 """ jsPath = r'./des.js' with open(jsPath, "r") as file: desJsCode = file.read() desJsFunction = execjs.compile(desJsCode) rsa = desJsFunction.call("strEnc", u + p + lt, "1", "2", "3") return rsa """ ---------------------------------------------------------------------------------- 第一次请求获取lt和execution的数值 """ header = { "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36' } loginUrl = """http://cas.upc.edu.cn/cas/login?service=http%3A%2F%2Fjwxt.upc.edu.cn%2Fneusoftcas.jsp""" session = requests.session() one = session.get(loginUrl) loginPageHtml = etree.HTML(one.text) lt = loginPageHtml.xpath(r'//*[@id="lt"]/@value')[0] execution = loginPageHtml.xpath(r'//*[@name="execution"]/@value')[0] u = '2009050216' p = '20020528lykLYK.' ul = len(u) pl = len(p) rsa = getRsa(u, p, lt) _eventId = "submit"
时间: 2023-10-23 20:47:27 浏览: 221
下面是相应的Dart代码实现:
```
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';
Future<String> getRsa(String u, String p, String lt) async {
/* 加密方法 */
File jsFile = File('./des.js');
String desJsCode = await jsFile.readAsString();
var context = await execjs.createContext(desJsCode);
String rsa = await context.callMethod("strEnc", [u + p + lt, "1", "2", "3"]);
return rsa;
}
Future<void> main() async {
/* 第一次请求获取lt和execution的数值 */
var headers = {
"User-Agent":
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'
};
var loginUrl =
"http://cas.upc.edu.cn/cas/login?service=http%3A%2F%2Fjwxt.upc.edu.cn%2Fneusoftcas.jsp";
var session = http.Client();
var response = await session.get(Uri.parse(loginUrl), headers: headers);
var document = parse(response.body);
var lt = document.querySelector('#lt')?.attributes['value'] ?? "";
var execution = document.querySelector('[name="execution"]')?.attributes['value'] ?? "";
var u = '2009050216';
var p = '20020528lykLYK.';
var rsa = await getRsa(u, p, lt);
var _eventId = "submit";
}
```
需要注意的是,由于Dart是一门静态类型语言,因此需要显式地声明变量类型。此外,Dart中没有像Python中的execjs库一样可以直接使用JavaScript代码,因此需要使用dart-js库来实现JavaScript代码的调用。
阅读全文