百度指数数据导出工具

这是一个 Tampermonkey 的脚本,用于将 baidu index 的数据导出为 csv

目前為 2024-07-04 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 百度指数数据导出工具
  3. // @namespace https://github.com/siaikin/baidu-index-export
  4. // @version 1.3.0
  5. // @author siaikin
  6. // @description 这是一个 Tampermonkey 的脚本,用于将 baidu index 的数据导出为 csv
  7. // @copyright https://github.com/siaikin
  8. // @homepage https://github.com/siaikin/baidu-index-export
  9. // @supportURL https://github.com/siaikin/baidu-index-export/issues
  10. // @match *://index.baidu.com/*
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. var ChartType = /* @__PURE__ */ ((ChartType2) => {
  18. ChartType2[ChartType2["TREND"] = 0] = "TREND";
  19. ChartType2[ChartType2["BRAND"] = 1] = "BRAND";
  20. return ChartType2;
  21. })(ChartType || {});
  22. function exportCSV(chartData) {
  23. const titleRow = ["name"].concat(chartData.xAxisData);
  24. const dataRows = chartData.series.map((item) => [`"${item.name}"`].concat(item.data));
  25. const csv = [titleRow.join(",") + "\n"];
  26. dataRows.forEach((row) => csv.push(row.join(",") + "\n"));
  27. download(
  28. new Blob(csv, { type: "text/csv" }),
  29. `${chartData.series.map((item) => item.name).join(",")}_${chartData.startDate}-${chartData.endDate}.csv`
  30. );
  31. }
  32. function exportNewsCSV(chartData) {
  33. const titleRow = ["name", "date", "newsDate", "newsSource", "newsTitle", "newsUrl"];
  34. let dataRows = [];
  35. for (const [name, dateList] of Object.entries(chartData.newDatas)) {
  36. const rows = dateList.map(({ xAxis, news }) => news.map((item) => [
  37. `"${name}"`,
  38. xAxis,
  39. `"${item.date}"`,
  40. `"${item.source}"`,
  41. `"${item.title}"`,
  42. `"${item.url}"`
  43. ])).flat();
  44. dataRows = dataRows.concat(rows);
  45. }
  46. const csv = [titleRow.join(",") + "\n"];
  47. dataRows.forEach((row) => csv.push(row.join(",") + "\n"));
  48. download(
  49. new Blob(csv, { type: "text/csv" }),
  50. `${chartData.series.map((item) => item.name).join(",")}_${chartData.startDate}-${chartData.endDate}.news.csv`
  51. );
  52. }
  53. function download(blob, filename) {
  54. const url = URL.createObjectURL(blob);
  55. const a = document.createElement("a");
  56. a.href = url;
  57. a.download = filename;
  58. a.click();
  59. URL.revokeObjectURL(url);
  60. a.remove();
  61. }
  62. async function loadExportTrendFeature(chartDataKey) {
  63. var _a, _b;
  64. let chartData = null;
  65. Object.defineProperty(
  66. Object.prototype,
  67. chartDataKey,
  68. {
  69. configurable: true,
  70. get: () => chartData,
  71. set(value) {
  72. delete Object.prototype[chartDataKey];
  73. chartData = value;
  74. Object.defineProperty(
  75. this,
  76. chartDataKey,
  77. {
  78. configurable: true,
  79. get: () => chartData,
  80. set(value2) {
  81. chartData = value2;
  82. }
  83. }
  84. );
  85. }
  86. }
  87. );
  88. await new Promise((resolve) => window.addEventListener("load", resolve));
  89. const container = document.querySelectorAll(".index-trend-content")[1];
  90. const splitline = container.querySelector(".splitline");
  91. const shareButton = container.querySelector(".share-icon");
  92. if (!splitline || !shareButton)
  93. return;
  94. const downloadButton = shareButton.cloneNode();
  95. downloadButton.innerHTML = '<a href="javascript:void(0)" class="share-icon">下载</a>';
  96. downloadButton.addEventListener("click", handleDownloadClick);
  97. (_a = shareButton.parentNode) == null ? void 0 : _a.append(splitline.cloneNode());
  98. (_b = shareButton.parentNode) == null ? void 0 : _b.append(downloadButton);
  99. function handleDownloadClick() {
  100. if (!chartData) {
  101. console.error("[baidu-index-export] chartData is not ready");
  102. return;
  103. }
  104. exportCSV(chartData.all);
  105. exportNewsCSV(chartData.all);
  106. }
  107. }
  108. async function loadExportFeedFeature(chartDataKey) {
  109. var _a, _b;
  110. let chartData = null;
  111. Object.defineProperty(
  112. Object.prototype,
  113. chartDataKey,
  114. {
  115. configurable: true,
  116. get: () => chartData,
  117. set(value) {
  118. delete Object.prototype[chartDataKey];
  119. chartData = value;
  120. Object.defineProperty(
  121. this,
  122. chartDataKey,
  123. {
  124. configurable: true,
  125. get: () => chartData,
  126. set(value2) {
  127. chartData = value2;
  128. }
  129. }
  130. );
  131. }
  132. }
  133. );
  134. await new Promise((resolve) => window.addEventListener("load", resolve));
  135. const container = document.querySelectorAll(".index-trend-content")[2];
  136. const splitline = container.querySelector(".splitline");
  137. const regionButton = container.querySelector(".index-region");
  138. if (!splitline || !regionButton)
  139. return;
  140. const downloadButton = regionButton.cloneNode();
  141. downloadButton.innerHTML = '<a href="javascript:void(0)" class="share-icon">下载</a>';
  142. downloadButton.addEventListener("click", handleDownloadClick);
  143. (_a = regionButton.parentNode) == null ? void 0 : _a.append(splitline.cloneNode());
  144. (_b = regionButton.parentNode) == null ? void 0 : _b.append(downloadButton);
  145. function handleDownloadClick() {
  146. if (!chartData) {
  147. console.error("[baidu-index-export] chartData is not ready");
  148. return;
  149. }
  150. exportCSV(chartData);
  151. exportNewsCSV(chartData);
  152. }
  153. }
  154. async function loadExportBrandFeature(chartDataKey) {
  155. var _a, _b;
  156. let chartData = null;
  157. let pageThis = null;
  158. Object.defineProperty(
  159. Object.prototype,
  160. chartDataKey,
  161. {
  162. configurable: true,
  163. get: () => chartData,
  164. set(value) {
  165. delete Object.prototype[chartDataKey];
  166. chartData = value;
  167. pageThis = this;
  168. Object.defineProperty(
  169. this,
  170. chartDataKey,
  171. {
  172. configurable: true,
  173. get: () => chartData,
  174. set(value2) {
  175. chartData = {
  176. ...chartData,
  177. ...value2,
  178. startDate: pageThis.startDate,
  179. endDate: pageThis.endDate
  180. };
  181. }
  182. }
  183. );
  184. }
  185. }
  186. );
  187. await new Promise((resolve) => window.addEventListener("load", resolve));
  188. const container = document.querySelectorAll(".index-brand-content")[0];
  189. const splitline = container.querySelector(".splitline");
  190. const shareButton = container.querySelector(".share-icon");
  191. if (!splitline || !shareButton)
  192. return;
  193. const downloadButton = shareButton.cloneNode();
  194. downloadButton.innerHTML = '<a href="javascript:void(0)" class="share-icon">下载</a>';
  195. downloadButton.addEventListener("click", handleDownloadClick);
  196. (_a = shareButton.parentNode) == null ? void 0 : _a.append(splitline.cloneNode());
  197. (_b = shareButton.parentNode) == null ? void 0 : _b.append(downloadButton);
  198. function handleDownloadClick() {
  199. if (!chartData) {
  200. console.error("[baidu-index-export] chartData is not ready");
  201. return;
  202. }
  203. exportCSV(chartData);
  204. }
  205. }
  206. const chartType = (() => {
  207. const hash = window.location.hash;
  208. if (hash.startsWith("#/trend/"))
  209. return ChartType.TREND;
  210. else if (hash.startsWith("#/brand/"))
  211. return ChartType.BRAND;
  212. else
  213. return ChartType.TREND;
  214. })();
  215. (() => {
  216. switch (chartType) {
  217. case ChartType.BRAND:
  218. loadExportBrandFeature("brandDatas");
  219. break;
  220. case ChartType.TREND:
  221. loadExportTrendFeature("chartDatas");
  222. loadExportFeedFeature("chartDatas2");
  223. break;
  224. }
  225. })();
  226.  
  227. })();

QingJ © 2025

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