本帖最后由 apang 于 2014-10-12 13:39 编辑
一楼本打算发下载zol中关村壁纸的,但代码写出来感觉好长,又罗嗦,权当练手吧。
如果有人能指点一二,感激不尽。- @set @n=0;// & cscript -nologo -e:jscript "%~0" & pause & exit/b
-
- var DstFD = 'D:\\壁纸';
- var url = 'http://desk.zol.com.cn';
- var arURL = [[],[]]; //定义二维数组
- var fso = new ActiveXObject('Scripting.FileSystemObject');
- var http = new ActiveXObject('Microsoft.XMLHTTP');
- //创建目录
- if (!fso.FolderExists(DstFD)) fso.CreateFolder(DstFD);
- //获取屏幕分辨率
- var WH = GetScreenPixel();
-
- var re = /href="(\/bizhi\/.+?\.html)".+?alt="(.+?)"/ig;
- //循环2次,下载第1~2页
- for (var i=1; i<=2; i++) {
- //壁纸分类:全部;壁纸列表:按推荐数
- http.Open('GET', url + '/pc/good_' + i + '.html', false);
- http.Send();
- var s = GetText(http.responseBody);
- var arr;
- while ((arr = re.exec(s)) != null) {
- //格式化子目录名
- arr[2] = arr[2].replace(/[\/\|\?\*:"<>\s\\]/g, '');
- arURL[0].push(arr[1]); //html地址放在数组第1维
- arURL[1].push(arr[2]); //子目录名称放在数组第2维
- }
- }
-
- var num = 0;
- var re = /href="(\/bizhi\/.+?)".+?"(http:[^=]+?144x90\/\d+\.jp(e)?g)"/ig;
- for (i=0; i<arURL[0].length; i++) {
- var fd = DstFD + '\\' +arURL[1][i];
- //创建子目录
- if (!fso.FolderExists(fd)) fso.CreateFolder(fd);
-
- http.Open('GET', url + arURL[0][i], false);
- http.Send();
- var s = GetText(http.responseBody);
- while ((arr = re.exec(s)) != null) {
- num += 1;
- try {
- http.Open('GET', url + arr[1], false);
- http.Send();
- var s1 = GetText(http.responseBody);
- //替换网址中的字符144x90
- arr[2] = arr[2].replace(/144x90/i, GetJpgPixel(s1));
- SaveToFile(arr[2], fd, num);
- } catch(e) { WScript.Echo(num + ' !! 失败 !!') }
- }
- }
- http = null; fso = null
-
- function GetText(bin) {
- with (new ActiveXObject("ADODB.Stream")) {
- Type = 1;
- Mode = 3;
- Open();
- Write(bin);
- Position = 0;
- Type = 2;
- Charset = 'GB2312';
- //删除“猜您喜欢”以下的内容
- return ReadText().split('<!--猜您喜欢-->')[0];
- }
- }
-
- //获取当前屏幕分辨率
- function GetScreenPixel() {
- var locator = new ActiveXObject('WbemScripting.SWbemLocator');
- var service = locator.ConnectServer('.');
- var p = service.ExecQuery('SELECT * FROM Win32_DesktopMonitor');
- var e = new Enumerator(p);
- for (; !e.atEnd(); e.moveNext()) {
- var w = e.item().ScreenWidth;
- var h = e.item().ScreenHeight;
- }
- return(w + 'x' + h)
- }
-
- //查找适合屏幕分辨率的JPG,如果找不到,取列表中的第一项(最大分辨率)
- function GetJpgPixel(txt) {
- var re = new RegExp('\\bid="' + WH + '"', 'i');
- if (txt.split(re).length == 1) {
- return(/\bid="(\d+x\d+)"/i.exec(txt)[1]);
- } else return(WH)
- }
-
- //下载、保存JPG
- function SaveToFile(strJpgURL, fd, num) {
- var file = strJpgURL.replace(/.+?(\d+\.jp(e)?g)$/i, '$1')
- WScript.Echo(num + ' 下载:' + fd + '\\' + file);
- http.Open('GET', strJpgURL, false);
- http.Send();
- with (new ActiveXObject('ADODB.Stream')) {
- Type = 1;
- Mode = 3;
- Open();
- Write(http.responseBody);
- SaveToFile(fd + '\\' + file, 2);
- }
- }
复制代码
|