Last active
October 3, 2017 15:16
-
-
Save brianpow/699ad7ce14b92f99c455ca8b7f1608ce to your computer and use it in GitHub Desktop.
Create a textarea with price and product details in tab-separated format in current category of www.price.com.hk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($) { | |
| var parse = function($$) { | |
| console.log("parse") | |
| var brands = $$.find("select[name=brand] option").map(function() { | |
| return this.value | |
| }).get() || [] | |
| brands.shift() | |
| var category = $$.find("div.nav-menu").text().trim() | |
| return $$.find("div.item").map(function() { | |
| let $$tr = $("div.line-04 tr", this); | |
| let brand = "" | |
| let data = [ | |
| $("div.line-01:eq(0)", this).text().trim(), | |
| $("div.line-02:eq(0)", this).text().trim(), | |
| $("div.price-range:eq(0) span.text-price-number:eq(0)", this).text().trim(), | |
| $("div.price-range:eq(0) span.text-price-number:eq(1)", this).text().trim(), | |
| $("div.price-range:eq(0) span.product-prop > img", this).attr("title"), | |
| $("div.price-range:eq(1) span.text-price-number:eq(0)", this).text().trim(), | |
| $("div.price-range:eq(1) span.text-price-number:eq(1)", this).text().trim(), | |
| $("div.price-range:eq(1) span.product-prop > img", this).attr("title") | |
| ] | |
| if (!$("a.btn", this).size()) | |
| return ""; | |
| $$tr.each(function() { | |
| let header = $("td:eq(0)", this).text().trim() | |
| if (headers.indexOf(header) == -1) | |
| headers.push(header) | |
| }) | |
| for (let header of headers) { | |
| data.push($$tr.filter(function() { | |
| return $("td:eq(0)", this).text().trim().indexOf(header) != -1 | |
| }).map(function() { | |
| return $("td:eq(1)", this).text().trim() | |
| }).get().join("")) | |
| } | |
| for (let b of brands) { | |
| if (data[0].indexOf(b) != -1) { | |
| brand = b | |
| break | |
| } | |
| } | |
| return [category, brand].concat(data).join("\t").trim() | |
| }).filter(function() { | |
| return this != "" | |
| }).get() | |
| } | |
| var findNext = function($$) { | |
| console.log("findNext") | |
| let $a = $$.find("ul.pagination li:last a:eq(0)") | |
| return ($a.text().indexOf("下一頁") != -1) ? $a.attr("href") : ""; | |
| } | |
| var loaded = function(data) { | |
| console.log("loaded") | |
| let $data = $(data) | |
| rows = rows.concat(parse($data)) | |
| let nextUrl = findNext($data) | |
| if (nextUrl) { | |
| console.log("Processing " + nextUrl) | |
| $.get(nextUrl, loaded) | |
| } else | |
| show(rows) | |
| } | |
| var show = function(result) { | |
| console.log("show") | |
| // console.log(result) | |
| if (!$("#result").size()) | |
| $("<textarea id=result />").css({ | |
| width: "100%", | |
| height: "100px" | |
| }).prependTo(document.body) | |
| $("#result").html( | |
| ["分類", "牌子", "牌子及型號", "簡介", "最低售價", "最高售價", "類型", "最低售價", "最高售價", "類型"].concat(headers).join("\t") + "\n" + result.join("\n") | |
| ) | |
| } | |
| var rows = [] | |
| var headers = [] | |
| rows = rows.concat(parse($(document.body))) | |
| nextUrl = findNext($(document.body)) | |
| if (nextUrl) { | |
| console.log("Processing " + nextUrl) | |
| $.get(nextUrl, loaded) | |
| } else | |
| show(rows) | |
| })(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment