一、目标任务:
要去 https://sjfw.scjs.net.cn:8801/xxgx/Enterprise/eList.aspx 根据企业名称查询到统一社会征信代码。
然后拿统一社会征信代码去 https://rzsc.sczwfw.gov.cn/portal/newOrg/orgRegister.jsp 查询法人信息。
最后两个页面不断跳转进行信息存储,最后导出数据。
二、实现代码
<!DOCTYPE html>
<html>
<div style="text-align: center;margin-top: 300px;">
<input type="file" id="input-excel" accept=".xls,.xlsx"/>
<button onclick="START()"
style="background-color: #4CAF50; color: white; padding: 15px 32px; text-align: center;
text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px;
cursor: pointer;">
执行获取社会统一信用代码
</button>
</div>
</html>
<script src="./xlsx/xlsx.js" charset="utf-8"></script>
<script>
// file:///C:/Users/xlliu24/Desktop/IT01/byCodeGetPhone.html
function START() {
const input = document.getElementById('input-excel');
const file = input.files[0];
if (!file) {
alert('请选择一个Excel文件');
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const data = e.target.result;
const workbook = XLSX.read(data, {type: 'array'});
const sheetName = workbook.SheetNames[0];
const sheet = workbook.Sheets[sheetName];
const jsonData = XLSX.utils.sheet_to_json(sheet);
console.log(jsonData);
let companyList = [];
for (var i = 0; i < jsonData.length; i++) {
companyList.push(jsonData[i].company);
}
window.name = "ALL_VAR=" + JSON.stringify(companyList) + "&index*0&[]";
// window.name只能存储一个变量,所以还必须用
setTimeout(function () {
window.open("https://sjfw.scjs.net.cn:8801/xxgx/Enterprise/eList.aspx",
"_self")
}, 1000)
};
reader.readAsArrayBuffer(file);
}
</script>
// ==UserScript==
// @name 搜索参数显示
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 显示搜索的参数
// @author You
// @include https://sjfw.scjs.net.cn:8801/*
// @include https://rzsc.sczwfw.gov.cn/*
// @grant none
// ==/UserScript==
let domainName = window.location.hostname;
// alert(domainName)
let ALL_VAR = getVarFromWindowName("ALL_VAR"); // 拿到所有变量
let varArray = ALL_VAR.split("&");
let companyList = JSON.parse(varArray[0]); // 公司名称-全局
let indexArray = varArray[1].split("*");
let index = parseInt(indexArray[1]); // 下标-全局
// alert(getCookie("clickSearch"))
if (!getCookie("clickSearch") == "1" && domainName == "sjfw.scjs.net.cn") { // 判断是否首次击过搜索
let companyInput = document.getElementById("mc");
companyInput.value = companyList[index]; // 下标已经从全局获取
let searchButton = document.getElementById("MainContent_Button1");
setTimeout(function () {
searchButton.click()
setCookie("clickSearch", "1", "1"); // 说明点击了搜索了
}, 500)
// alert("执行1")
// alert("我应该在跳转后执行的...")
} else {
let code = "";
// 执行点击以后要去拿社会征信代码
setTimeout(function () {
if (domainName == "sjfw.scjs.net.cn") {
let tempList = document.getElementsByClassName("text-center");
let codeList = [];
// alert(JSON.stringify(varArray[2]))
console.log(JSON.stringify(varArray[2]))
if (varArray[2] == "[]") {
// alert("varArray[2] 不改变")
console.log("不改变")
} else {
// alert("varArray[2] 改变")
codeList = JSON.parse(varArray[2]); // 公司名称-全局
}
code = tempList[tempList.length - 1].innerText;
let companyInfo = {
"name": companyList[index], "code": code
}
codeList.push(companyInfo);
// 同时更新记录
window.name = "ALL_VAR=" + JSON.stringify(companyList) + "&index*" + index + "&" + JSON.stringify(codeList);
setTimeout(function () {
clearCookie("clickSearch"); // 需要放到另外一个地方清理
window.open("https://rzsc.sczwfw.gov.cn/portal/newOrg/orgRegister.jsp", "_self")
}, 200)
}
// alert(code)
}, 1000)
}
if (domainName == "rzsc.sczwfw.gov.cn") {
if (varArray[2] == "[]") {
// alert("varArray[2] 不改变")
console.log("不改变")
} else {
// alert("varArray[2] 改变")
codeList = JSON.parse(varArray[2]); // 公司名称-全局
}
setTimeout(function () {
// alert("执行了填充")
// 跳转页面后一切都要重新获取
let varArray = ALL_VAR.split("&");
let indexArray = varArray[1].split("*");
let index = parseInt(indexArray[1]); // 下标-全局
getPhone(codeList[index].code);
}, 1000)
}
// 1、从window.name获取全局变量
function getVarFromWindowName(varName) {
var nameValuePairs = window.name.split(';');
for (var i = 0; i < nameValuePairs.length; i++) {
var pair = nameValuePairs[i].split('=');
if (pair[0] === varName) {
return pair[1];
}
}
return null;
}
// 2、获取cookie
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function clearCookie(cookieName) {
document.cookie = cookieName + "=; expires=" + new Date(0).toUTCString() + "; path=/";
}
//3、从新页面根据code拿到手机号
// 320381198812252138
function getPhone(code) {
let deptName = document.getElementById("deptName");
let socialCreditCode = document.getElementById("socialCreditCode");
let deptPerson = document.getElementById("deptPerson");
deptName.value = code;
socialCreditCode.value = code;
deptPerson.value = code;
let deptPersonCode = document.getElementById("deptPersonCode");
deptPersonCode.value = "320381198812252138";
deptPersonCode.focus();
deptPersonCode.blur();// 失去焦点
setTimeout(function () {
let deptPersonCodeMsg = document.getElementById("deptPersonCodeMsg");
// alert(deptPersonCodeMsg.innerText)
// alert(deptPersonCodeMsg.innerText.substring(18, 29))
let codeList = [];
if (varArray[2] == "[]") {
// alert("varArray[2] 不改变")
console.log("不改变")
} else {
// alert("varArray[2] 改变")
codeList = JSON.parse(varArray[2]); // 公司名称-全局
}
codeList[index].phone = deptPersonCodeMsg.innerText.substring(18, 29);
if (deptPersonCodeMsg.innerText.substring(18, 29).startsWith("信")) {
codeList[index].phone = "无";
}
if (index < companyList.length - 1) {
index = index + 1;
} else {
// 调用导出
// alert("最终结果" + JSON.stringify(companyList) + "---" + index + "---" + JSON.stringify(codeList));
part_4(codeList)
return false;
}
// alert(JSON.stringify(codeList))
window.name = "ALL_VAR=" + JSON.stringify(companyList) + "&index*" + index + "&" + JSON.stringify(codeList);
ALL_VAR = getVarFromWindowName("ALL_VAR"); // 拿到所有变量
varArray = ALL_VAR.split("&");
companyList = JSON.parse(varArray[0]); // 公司名称-全局
indexArray = varArray[1].split("*");
index = parseInt(indexArray[1]); // 下标-全局
if (varArray[2] == "[]") {
// alert("varArray[2] 不改变")
console.log("不改变")
} else {
// alert("varArray[2] 改变")
codeList = JSON.parse(varArray[2]); // 公司名称-全局
}
// alert(JSON.stringify(companyList) + "---" + index + "---" + JSON.stringify(codeList));
window.open("https://sjfw.scjs.net.cn:8801/xxgx/Enterprise/eList.aspx", "_self")
}, 3000)
}
// 4、导出为csv
function part_4(my_export_data) { // 触发导出
//要导出的json数据
const jsonData = my_export_data;
//列标题,逗号隔开,每一个逗号就是隔开一个单元格
let str = `公司名称,社会征信代码,手机号\n`;
//增加\t为了不让表格显示科学计数法或者其他格式
for (let i = 0; i < jsonData.length; i++) {
for (let item in jsonData[i]) {
str += `${jsonData[i][item] + '\t'},`;
}
str += '\n';
}
//encodeURIComponent解决中文乱码
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
//通过创建a标签实现
let link = document.createElement("a");
link.href = uri;
//对下载的文件命名
link.download = "社会征信代码查询手机号导出的明细.csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
三、最终存储与拿到的数据如下: