1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /*原價屋機殼列表轉 SQL 程式*/ //載入 Zepto.js let script = document.createElement('script'); script.src = 'https://zeptojs.com/zepto.min.js'; document.head.append(script) function a(r,x,d=undefined,i=1){ var rr = r.exec(x); if(rr) return rr[i]; return d; } //name,mbsize,size1,size2,size3,waterColder,coolerSize,hdd,special,price var list = []; $('.main span').forEach((item)=>{ var info = {}; $(item).find('div').forEach((spec)=>{ var text = spec.innerText; var rr; //XPG DEFENDER PRO 白 顯卡長38/CPU高17/磁吸網孔面板/玻璃透側/E-ATX if(spec.className == 't'){ info.name = text; info.mbsize = a(/(ATX|E-ATX|M-ATX|ITX)/,text); info.special = a(/(SFX|工業機殼)/,text); if(info.special=='工業機殼') info.mbsize = a(/(\dU)/,text); info.coolerSize = a(/U高 ?(\d+\.?\d*)/,text); return; } rr = /^尺寸 ?(:|:) ?(\d+\.?\d*)\*(\d+\.?\d*)\*(\d+\.?\d*)/.exec(text); if(rr){ var s1 = parseFloat(rr[2]); var s2 = parseFloat(rr[3]); var s3 = parseFloat(rr[4]); if( s1 < s2 ){var ss = s1; s1 = s2; s2 = ss; } if( s2 < s3 ){var ss = s2; s2 = s3; s3 = ss; } if( s1 < s2 ){var ss = s1; s1 = s2; s2 = ss; } info.s1 = s1; info.s2 = s2; info.s3 = s3; return; } //支援水冷散熱排:360/280/240/120 rr = a(/^支援水冷散熱排 ?(:|:) ?(.*)$/,text,null,2); if(rr){ info.waterColder = rr; return; } //硬碟空間:3.5 or 2.5*2/2.5*2 rr = a(/^硬碟空間 ?(:|:) ?(.*)$/,text,null,2); if(rr){ info.hdd = rr; return; } rr = a(/^含稅價 ?(:|:) ?NT ?(\d*)/,text,null,2); if(rr){ info.price = rr; info.url = $(item).find('div.x a').attr('href'); return; } }) list.push(info); }) function sl(v,d='NULL',t=String){ var qu = t === String; if(v===null||v===undefined) v = d; if(v===null||v===undefined){ v = 'NULL'; qu = false; } else if(v===true||v===false){v = v?1:0; qu=false; } v = String(v).replace(/'/g,"\\'"); return `${qu?"'":""}${v}${qu?"'":""}`; } var sql = 'INSERT INTO `cases`(`name`,`mbsize`,`size1`,`size2`,`size3`,`waterColder`,`coolerSize`,`hdd`,`special`,`price`,`url`) VALUES\n'; list.forEach((item)=>{ sql += '('+ `${sl(item.name)}, ` + `${sl(item.mbsize)}, ` + `${sl(item.s1,0,Number)}, ` + `${sl(item.s2,0,Number)}, ` + `${sl(item.s3,0,Number)}, ` + `${sl(item.waterColder)}, ` + `${sl(item.coolerSize,null,Number)}, ` + `${sl(item.hdd)}, ` + `${sl(item.special)}, ` + `${sl(item.price,0,Number)}, ` + `${sl(item.url)} ` + '),\n'; }); sql = sql.substr(0,sql.length-2)+';'; console.log(sql); /* 建立表格用 CREATE TABLE `cases` ( `name` TINYTEXT NULL, `mbsize` VARCHAR(32) NULL, `size1` FLOAT DEFAULT 0 NOT NULL, `size2` FLOAT DEFAULT 0 NOT NULL, `size3` FLOAT DEFAULT 0 NOT NULL, `waterColder` TINYTEXT NULL, `coolerSize` FLOAT NULL, `hdd` TINYTEXT NULL, `special` TINYTEXT NULL, `price` INT NULL, `url` TEXT NULL, INDEX(mbsize), INDEX(size1), INDEX(size2), INDEX(size3), INDEX(waterColder), INDEX(coolerSize), INDEX(special), INDEX(price) ) CHARSET=utf8 COLLATE utf8_general_ci; */ |
Direct link: https://paste.plurk.com/show/rf3BuIt84hhG0uWC8LcA