Anyway, challenge: accepted. Save this with a .bat extension. It'll look for a text file containing SKUs, one per line, and fetch and scrape the search page for each, writing info from the first anchor element with a .className of "product-image" to a CSV file.@if (@CodeSection == @Batch) @then@echo offsetlocalset "skufile=sku.txt"set "outfile=output.csv"set "URL=http://www.aidangrayhome.com/catalogsearch/result/?q="rem // invoke JScript portioncscript /nologo /e:jscript "%~f0" "%skufile%" "%outfile%" "%URL%"echo Done.rem // end main runtimegoto :EOF@end // end batch / begin JScript chimeravar fso = WSH.CreateObject('scripting.filesystemobject'), skufile = fso.OpenTextFile(WSH.Arguments(0), 1), skus = skufile.ReadAll().split(/\r?\n/), outfile = fso.CreateTextFile(WSH.Arguments(1), true), URL = WSH.Arguments(2);skufile.Close();String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }// returns a DOM root objectfunction fetch(url) { var XHR = WSH.CreateObject("Microsoft.XMLHTTP"), DOM = WSH.CreateObject('htmlfile'); WSH.StdErr.Write('fetching ' + url); XHR.open("GET",url,true); XHR.setRequestHeader('User-Agent','XMLHTTP/1.0'); XHR.send(''); while (XHR.readyState!=4) {WSH.Sleep(25)}; DOM.write(XHR.responseText); return DOM;}function out(what) { WSH.StdErr.Write(new Array(79).join(String.fromCharCode(8))); WSH.Echo(what); outfile.WriteLine(what);}WSH.Echo('Writing to ' + WSH.Arguments(1) + '...')out('sku,product,URL');for (var i=0; i<skus.length; i++) { if (!skus[i]) continue; var DOM = fetch(URL + skus[i]), anchors = DOM.getElementsByTagName('a'); for (var j=0; j<anchors.length; j++) { if (/\bproduct-image\b/i.test(anchors[j].className)) { out(skus[i]+',"' + anchors[j].title.trim() + '","' + anchors[j].href + '"'); break; } }}outfile.Close();可惜的是htmlfile COM对象不支持getElementsByClassName. :/但这在我的测试中似乎已经足够好了.Too bad the htmlfile COM object doesn't support getElementsByClassName. :/ But this seems to work well enough in my testing. 这篇关于给定网站中的SKU编号列表,提取产品名称的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 13:07
查看更多