Steam Wishlist Scraper

Scrapes steam wishlist into a CSV file for Excel / LibreOffice

目前为 2020-02-29 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Steam Wishlist Scraper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Scrapes steam wishlist into a CSV file for Excel / LibreOffice
  6. // @author retnuh66@gmail.com
  7. // @match *store.steampowered.com/wishlist/profiles/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. ( function() {
  12. 'use strict';
  13.  
  14. var titles = [];
  15. var dates = [];
  16.  
  17. var final_output = [];
  18.  
  19. checkData();
  20.  
  21. // Scroll and collect data until we hit the bottom of the page
  22. function scrollScrape(){
  23. if ((window.innerHeight + window.scrollY) < document.body.offsetHeight) {
  24. scrapeData();
  25. setTimeout(function() {
  26. scrollScrape();
  27. }, 100);
  28. } else {
  29. console.log("End");
  30. doneScraping();
  31. }
  32. }
  33.  
  34. // Scrape titles and release dates
  35. function scrapeData(){
  36. titles = document.getElementsByClassName('title');
  37. dates = document.getElementsByClassName('value release_date');
  38.  
  39. var output = [];
  40. var clean_output = [];
  41. console.log(titles);
  42.  
  43. for(var i = 0; i < titles.length; i++){
  44. var temp_title = titles[i].innerText;
  45. var temp_date = dates[i].innerText;
  46. //console.log(temp_title);
  47. //console.log(temp_date);
  48. if (typeof temp_title != 'undefined' && temp_title.length != 0 && typeof temp_date != 'undefined' && temp_date.length != 0){
  49. // Strip commas, add "^" for newlines
  50. temp_title = "^" + temp_title.replace(/,/g,"");
  51. temp_date = temp_date.replace(/,/g,"");
  52. // Trim whitespace
  53. temp_title = temp_title.trim();
  54. temp_date = temp_date.trim();
  55.  
  56. if ( !final_output.includes(temp_title)){
  57. output.push(temp_title);
  58. output.push(temp_date);
  59. }
  60. }
  61. }
  62.  
  63. clean_output = cleanArray(output);
  64. final_output = final_output.concat(clean_output);
  65. console.log(final_output);
  66.  
  67. window.scrollBy(0, 500);
  68. }
  69.  
  70. // Waits until the collection is populated by wishlist.js
  71. function checkData(){
  72. titles = document.getElementsByClassName('title');
  73. dates = document.getElementsByClassName('value release_date');
  74. if (typeof titles[0] == 'undefined' || titles[0] == null){
  75. console.log("waiting for wishlist.js...");
  76. setTimeout(function() {
  77. checkData();
  78. }, 1000);
  79. } else {
  80. console.log("wishlist.js completed.");
  81. scrollScrape();
  82. }
  83. }
  84.  
  85. // Removes "undefined" and null entries in the array
  86. function cleanArray(arr) {
  87. var len = arr.length, i;
  88.  
  89. for(i = 0; i < len; i++ ) {
  90. if (arr[i] && typeof arr[i] != 'undefined') {
  91. arr.push(arr[i]); // copy non-empty values to the end of the array
  92. }
  93. }
  94.  
  95. arr.splice(0 , len); // cut the array and leave only the non-empty values
  96.  
  97. return arr;
  98. }
  99.  
  100. // Generate CSV file and download
  101. function downloadCSV(args) {
  102. var data, filename, link;
  103. // Remove leading '^'
  104. final_output[0] = final_output[0].slice(1, final_output[0].length);
  105. var csv = final_output.toString();
  106.  
  107. if (csv == null) return;
  108.  
  109. // Replaces "^" with newlines
  110. csv = csv.replace( /\^/g, "\n");
  111. csv = csv.replace(/,,/g, ',');
  112.  
  113. console.log(csv);
  114.  
  115. filename = 'wishlist.csv';
  116.  
  117. if (!csv.match(/^data:text\/csv/i)) {
  118. csv = 'data:text/csv;charset=utf-8,' + csv;
  119. }
  120. data = encodeURI(csv);
  121.  
  122. link = document.createElement('a');
  123. link.setAttribute('href', data);
  124. link.setAttribute('download', filename);
  125. link.click();
  126. }
  127.  
  128. function doneScraping(){
  129. window.scrollTo(0,0);
  130.  
  131. console.log(final_output);
  132. console.log("Done!");
  133.  
  134. downloadCSV();
  135. }
  136.  
  137. })();

QingJ © 2025

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