排名查询

搜索引擎排名查询

目前为 2023-07-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 排名查询
  3. // @namespace http://blog.sxnxcy.com/
  4. // @version 1.0.1
  5. // @description 搜索引擎排名查询
  6. // @author xiaobao
  7. // @license CC-BY-4.0
  8. // @run-at document-start
  9. // @grant GM_addStyle
  10. // @grant GM_getValue
  11. // @grant GM_deleteValue
  12. // @grant GM_getResourceURL
  13. // @grant GM_openInTab
  14. // @grant GM_xmlhttpRequest
  15. // @grant GM_notification
  16. // @match *://*.baidu.com/*
  17. // @match *://*so.toutiao.com/*
  18. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js
  19. // @require https://unpkg.com/layui@2.8.6/dist/layui.js
  20. // @require https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.core.min.js
  21.  
  22. // ==/UserScript==
  23.  
  24. (function () {
  25. console.log("脚本注入成功", GM_info.script.version);
  26. GM_addStyle(`@import url('https://unpkg.com/layui@2.8.6/dist/css/layui.css');
  27. .chrome-plugin-demo-panel {
  28. position: fixed;
  29. right: 0;
  30. bottom: 10px;
  31. padding: 10px;
  32. width: 500px;
  33. height: 800px;
  34. margin-right: 30px;
  35. }
  36. #pzdv{
  37. position: fixed;
  38. right: 0;
  39. bottom: 30px;
  40. padding: 40px;
  41. margin-right: 40px;
  42. }
  43. `);
  44. })();
  45. let nbHtml = `
  46. <div class="layui-field-box">
  47. <div class="layui-row layui-col-space5">
  48. <div class="layui-col-xs4">
  49. <input type="text" id="ys" placeholder="搜索页数" class="layui-input" value="1">
  50. </div>
  51. <div class="layui-col-xs4">
  52. <input type="text" id="sl" placeholder="每页数量" class="layui-input" value="50">
  53. </div>
  54. </div>
  55. <div class="layui-row">
  56. <div class="layui-col-xs12">
  57. <textarea id="gjc" placeholder="请输入关键词" class="layui-textarea"
  58. style="height: 200px;">九方智投 site:www.9fzt.com</textarea>
  59. </div>
  60. <div class="layui-col-xs12">
  61. <textarea id="ym" placeholder="域名或者链接" class="layui-textarea"
  62. style="height: 200px;">www.9fzt.com</textarea>
  63. </div>
  64. </div>
  65. <div class="layui-row">
  66. <hr>
  67. <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="demo-filter-progress">
  68. <div id="jdt" class="layui-progress-bar" lay-percent="0%">
  69. </div>
  70. </div>
  71. </div>
  72. <div class="layui-row layui-col-space5">
  73. <hr>
  74. <button id="saveStart" class="layui-btn layui-btn-primary layui-border-blue">开始</button>
  75. <button id="butTest" class="layui-btn layui-btn-primary layui-border-red">下载结果</button>
  76. <div class="layui-form">
  77. <input id="checkbox1" type="checkbox" name="" title="只取一条">
  78. </div>
  79. </div>
  80. </div>
  81. `;
  82. let zjg = []
  83. let zarr = []
  84. document.addEventListener('DOMContentLoaded', function () {
  85. insertPage();
  86. localStorage.yzm = 1
  87. $("#pzan").click(function () {
  88. layer.open({
  89. type: 1,
  90. title: "排名查询配置",
  91. offset: 'auto',
  92. anim: 'slideLeft', // 从右往左
  93. area: ['750px', '50%'],
  94. shade: 0.1,
  95. shadeClose: false,
  96. id: 'ID-demo-layer-direction-r',
  97. content: nbHtml
  98. });
  99. layui.use('form', function () {
  100. var form = layui.form;
  101. form.render()
  102. });
  103. configurationButtonEvent()
  104. });
  105. })
  106. function sendMessage(title, message) {
  107. GM_notification({
  108. text: message,
  109. title: title,
  110. timeout: 5000, // 通知显示时间,单位为毫秒,默认为 4000 毫秒
  111. onclick: function () {
  112. }
  113. });
  114. }
  115. // 配置按钮事件
  116. function configurationButtonEvent() {
  117. $("#saveStart").click(() => {
  118. setTimeout(function () {
  119. rw($("#gjc").val(), $("#ym").val())
  120. }, 500)
  121. })
  122. $("#butTest").click(function () {
  123. xz();
  124. });
  125. }
  126. // 进度条更新
  127. function jdtup(bfb) {
  128. layui.element.progress('demo-filter-progress', bfb + "%")
  129. document.querySelector("#jdt").innerText = bfb + "%"
  130. }
  131. //插入配置按钮页
  132. function insertPage() {
  133. var panel = document.createElement('div');
  134. panel.id = 'pzdv';
  135. let html = `<button id="pzan" type="button" class="layui-btn layui-btn-primary" lay-on="test-offset-r"></button>`
  136. panel.innerHTML = html
  137. document.body.appendChild(panel);
  138. document.querySelector("#pzan").innerText = "排名查询 当前版本:" + GM_info.script.version
  139. }
  140. // 任务处理
  141. async function rw(gjc, ym) {
  142. zjg = []
  143. zarr = [["关键词", "排名", "域名", "真实地址", "标题"]]
  144. sendMessage('排名查询', '任务开始');
  145. jdtup(0)
  146. let ys = Number(document.querySelector("#ys").value)
  147. let sl = Number(document.querySelector("#sl").value)
  148. let gjcsz = gjc.trim().split("\n")
  149. let ymsz = ym.trim().split("\n")
  150. if (gjcsz.length != ymsz.length) {
  151. sendMessage('排名查询', '关键词域名数量不对应任务结束');
  152. return
  153. }
  154. for (let index = 0; index < gjcsz.length; index++) {
  155. console.log("当前执行:" + gjcsz[index]);
  156. await singleTaskProcessing(gjcsz[index], ymsz[index], ys, sl)
  157. jdtup(Math.floor((index + 1) / gjcsz.length * 100))
  158. await delayedAction()
  159. }
  160. sendMessage('排名查询', '任务完成');
  161. console.log("结果预览:", zjg);
  162. }
  163.  
  164. // 单任务处理
  165. async function singleTaskProcessing(gjc, ym, ys, sl) {
  166. switch (location.host) {
  167. case "www.baidu.com":
  168. baiduPc(gjc, ym, ys, sl)
  169. break;
  170. case "m.baidu.com":
  171. baiduYd(gjc, ym, ys, sl)
  172. break;
  173. case "so.toutiao.com":
  174. touduPc(gjc, ym, ys, sl)
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180.  
  181. async function baiduPc(gjc, ym, ys, sl) {
  182. let dt = document.getElementById('checkbox1').checked
  183. for (let index = 0; index < ys; index++) {
  184. url = "https://www.baidu.com/s?wd=" + encodeURIComponent(gjc) + "&pn=" + index * sl + "&rn=" + sl
  185. let str = await syncGet2(url) //json接口匹配
  186. let parser = new DOMParser();
  187. let doc = parser.parseFromString(str, 'text/html');
  188. let bqsz = doc.querySelectorAll("#content_left [srcid]");
  189. for (let bq of bqsz) {
  190. if (bq.querySelector("H3") != null) {
  191. let dz = bq.getAttribute("mu")
  192. let pm = bq.getAttribute("id")
  193. let bt = bq.querySelector("H3").innerText
  194. if (getDomain(dz, ym)) {
  195. zarr.push([gjc, pm, ym, dz, bt])
  196. zjg.push([gjc, pm, ym, dz, bt])
  197. if (dt) {
  198. return //取单条返回
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. async function baiduYd(gjc, ym, ys, sl) {
  206. let dt = document.getElementById('checkbox1').checked
  207. for (let index = 0; index < ys; index++) {
  208. url = "https://m.baidu.com/s?word=" + encodeURIComponent(gjc) + "&pn=" + index * sl
  209. let str = await syncGet2(url) //json接口匹配
  210. let parser = new DOMParser();
  211. let doc = parser.parseFromString(str, 'text/html');
  212. let bqsz = doc.querySelectorAll(".c-result.result");
  213. for (let bq of bqsz) {
  214. if (bq.querySelector("H3") != null) {
  215. let xjson = JSON.parse(bq.getAttribute("data-log"))
  216. let dz = xjson.mu
  217. let pm = xjson.order + index * 10
  218. let bt = bq.querySelector("H3").innerText
  219. if (getDomain(dz, ym)) {
  220. zarr.push([gjc, pm, ym, dz, bt])
  221. zjg.push([gjc, pm, ym, dz, bt])
  222. if (dt) {
  223. return //取单条返回
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230.  
  231. async function touduPc(gjc, ym, ys, sl) {
  232. let dt = document.getElementById('checkbox1').checked
  233. for (let index = 0; index < ys; index++) {
  234. url = "https://so.toutiao.com/search?dvpf=pc&source=pagination&keyword=" + encodeURIComponent(gjc) + "&page_num=" + (index)
  235. let str = await syncGet2(url) //json接口匹配
  236. let parser = new DOMParser();
  237. let doc = parser.parseFromString(str, 'text/html');
  238. let bqsz = doc.querySelectorAll("div.s-result-list div.result-content[data-i]");
  239. for (let bq of bqsz) {
  240. if (bq.querySelector("[data-log-extra]") != null) {
  241. let xjson = JSON.parse(bq.querySelector("[data-log-extra]").getAttribute("data-log-extra"))
  242. let dz = ""
  243. console.log(bq.querySelector("[data-log-extra]").getAttribute("data-log-extra"));
  244. if (xjson.hasOwnProperty("url")) {
  245. dz = xjson.url
  246. } else {
  247. if (xjson.result_type == "self_video") {
  248. dz = "https://www.toutiao.com/video/" + xjson.search_result_id + "/?channel=&source=search_tab"
  249. }
  250. if (xjson.result_type == "self_article") {
  251. dz = "https://www.toutiao.com/article/" + xjson.search_result_id + "/?channel=&source=search_tab"
  252. }
  253. if (xjson.result_type == "user") {
  254. dz = xjson.common_params.items[0].doc_url
  255. }
  256.  
  257. }
  258. let pm = Number(bq.getAttribute("data-i")) * (index + 1) + 1
  259. let bt = bq.querySelector("a").innerText
  260. if (getDomain(dz, ym)) {
  261. zarr.push([gjc, pm, ym, dz, bt])
  262. zjg.push([gjc, pm, ym, dz, bt])
  263. if (dt) {
  264. return //取单条返回
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. // 延迟
  272. function sleep(ms) {
  273. return new Promise((resolve) => {
  274. setTimeout(resolve, ms);
  275. });
  276. }
  277. //延迟执行
  278. async function delayedAction(s) {
  279. if (s == null | s == undefined) {
  280. s = 1000
  281. }
  282. await sleep(s);
  283. }
  284. async function xz() {
  285. const wb = XLSX.utils.book_new();
  286. const ws = XLSX.utils.aoa_to_sheet(zarr);
  287. XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
  288. const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
  289. const blob = new Blob([wbout], { type: 'application/octet-stream' });
  290. const downloadLink = document.createElement('a');
  291. downloadLink.id = "downloadLink"
  292. downloadLink.href = URL.createObjectURL(blob);
  293. downloadLink.download = 'data.xlsx';
  294. downloadLink.click();
  295. downloadLink.remove()
  296. }
  297. async function syncGet2(url) {
  298. let xhr = null
  299. while (true) {
  300. if (localStorage.yzm == "1") {
  301. xhr = new XMLHttpRequest();
  302. xhr.open('GET', url, false); // 同步请求
  303. xhr.send();
  304. }
  305. if (localStorage.yzm == "1" && xhr && (xhr.responseURL.indexOf("wappass.baidu.com") > -1)) {
  306. localStorage.yzm = "0"
  307. sendMessage("百度搜索", "请手动处理验证码")
  308. window.open(xhr.responseURL)
  309. } else { //ok
  310. if (xhr && xhr.responseURL.indexOf("wappass.baidu.com") == -1) {
  311. return xhr.responseText
  312. }
  313. }
  314. if (localStorage.yzm == "0") {
  315. console.log("等待验证码处理中...");
  316. await delayedAction(10000)
  317. debugger
  318. }
  319. }
  320. }
  321.  
  322.  
  323. // 链接对比模糊对比
  324. function getDomain(url, url2) {
  325. url = url.replaceAll("http://", "").replaceAll("https://", "")
  326. url2 = url2.replaceAll("http://", "").replaceAll("https://", "")
  327. return url.indexOf(url2) > -1
  328. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址