一键下载商品信息

简爱代码>JavaScript2016-10-28 8:0859853

最近工作越来越离谱,先是 打印文件,这次又安排发布商品信息,给几个 某宝的链接就让发布也是醉了,现在程序员都干这些了?

没办法,安排啥干啥吧,去某宝一看商品信息不多,主要是详情图片, 而自己要发布的地方信息也差不多


所以就写了一个下载某宝信息的 脚本,拖动 保存商品信息到浏览器书签工具栏,在商品详情页面点击保存的书签即可,


具体代码如下

javascript:(function(){
	/*@ 
	 * 下载淘宝、天猫商品信息
	 * 包括图文详情产品图片
	 *
	 * 20161028
	 * 简爱博客 http://www.gouji.org
	**/
	var rules  = { /* 所有规则 */
		tm: {
			reg: /:\/\/detail\.tmall\.com\//, /* 网址匹配 */
			title: "#J_DetailMeta h1", /* 标题选择器 */
			thumb: "#J_ImgBooth", /* 缩略图 */
			images: "#J_UlThumb img", /* 产品图片 */
			price: ".tm-price", /* 价格 */
			content: ".content", /* 图文详情 */
			imgReg: /\.jpg$/ /* 详情图片图片地址匹配规则 可以留空 */
		},
		tb: {
			reg: /:\/\/item\.taobao\.com\//,
			title: ".tb-title h3",
			thumb: "#J_ImgBooth",
			images: "#J_UlThumb img",
			price: "#J_PromoPriceNum",
			content: ".content",
			imgReg: /\.jpg$/
		}
	};


	window.URL = window.URL || window.webkitURL;
	var rule = !1, /* 当前规则 */
	iSpider = !1, /* 是否可以抓取 */
	i = {}; /* 存储返回信息 */

	for(k in rules){
		if(rules[k].reg.test(window.location.href)){
			rule = rules[k];
			break;
		}
	}

	if(rule){
		if(!iSpider){
			document.body.scrollTop = 9999;
			setTimeout(spider, 800);
			iSpider = !0;
		}
	}else{
		return alert("请在 天猫 或者 淘宝详情页面 使用此工具");
	}


	function spider(){
		i.title = t(g(rule.title).innerText);
		i.price = parseFloat(t(g(rule.price).innerText));
		i.score = i.price * 100 + 300;
		i.image = g(rule.thumb).src;
		i.content = g(rule.content).innerText.replace(/(^\s+|\s+$|[ \t]+)/g, " ");
		i.author = "\u7b80\u7231\u535a\u5ba2 http:\/\/www.gouji.org";
		i.html = g(rule.content).innerHTML;
		/* 获取所有内容 */
		i.images = getImage(g(rule.images, 1));
		i.content_images = getImage(g(rule.content).querySelectorAll("img"));

		text = [i.author,"","\u6807\u9898",i.title,"","\u4ef7\u683c",i.price,"","\u7f29\u7565\u56fe",i.image,"","\u4ea7\u54c1\u56fe\u7247",i.images.join("\r\n"),"","\u8be6\u60c5\u56fe\u7247",i.content_images.join("\r\n"),"","\u7eaf\u6587\u7b80\u4ecb",i.content,"","\u56fe\u6587\u8be6\u60c5",i.html].join("\r\n");
		download(text, i.title +".txt");
		download(JSON.stringify(i), i.title +".json");
		download(i.image, i.title +".jpg", true);
		for(k in i.content_images){download(i.content_images[k], i.title +"--content_"+ k +".jpg", true);}
		for(k in i.images){download(i.images[k], i.title +"---"+ k +".jpg", true);}
	}


	function g(q, s){return s ? document.querySelectorAll(q) : document.querySelector(q);}
	function t(q){return (q+"").replace(/(^\s+|\s+$)/g, "");}

	/* 下载指定内容 内容, 保存文件名, URL 文件是否强制命名 (资源在同域名 或者 支持跨域) */
	function download(t, n){
		if(!t || !n){
			return false;
		}

		if(/^https{0,1}:\/\/.+$/.test(t)){
			url = t;
		}else{
			blob = new Blob([t], {type:"text/html"});
			url = window.URL.createObjectURL(blob);
		}
		a = document.createElement("a");
	  a.href = url;
		a.download = n;
		a.click();
	}

	/* 通过 dom 元素返回 图片地址 */
	function getImage(images){
		var img_map = {}, _imgs = [];
		imgAtr = ["data-original", "data-src", "data-ks-lazyload", "src"];
		for(k in images){
			image = images[k];
			if(!image || !image.getAttribute){
				continue;
			}

			for(b in imgAtr){
				img = image.getAttribute(imgAtr[b]);
				img = (img+"").indexOf("//") == 0 ? window.location.protocol+img : img;
				if(!rule.imgReg || rule.imgReg.test(img)){
					break;
				}else{
					img = false;
				}
			}
			if(img && !img_map[img]){
				_imgs.push(img);
				img_map[img] = 1;
			}
		}
		return _imgs;
	}

})();

本文出自简爱博客,转载时请注明出处及相应链接。

评论

  1. 新小圈2016-12-09 15:31回复

    [赞/]

  2. 柏小白2016-11-24 10:13回复

    学习站点内容中,相互交流

  3. xuqiudong2016-11-21 12:31回复

    [哈哈/]

发表评论

电子邮件地址不会被公开。必填项已用*标注