[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 WHY 于 2018-11-19 20:19 编辑

分三个脚本,第1个脚本 startScript.bat,第 2 个脚本 startScript.ps1,第 3 个脚本 DownLoadURL.PS1,三个脚本存放在同一目录。
双击 startScript.bat 运行。

startScript.bat
  1. @echo off
  2. echo;Wait ...
  3. PowerShell -exec ByPass -f "startScript.ps1"
  4. echo;Done
  5. pause
复制代码
startScript.ps1
修改  startScript.ps1 第11行变量 $i 的取值范围,这里是从第一页到第100页
  1. $SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault();
  2. $Pool = [RunspaceFactory]::CreateRunspacePool(1, 5, $SessionState, $Host);
  3. $Pool.Open();
  4. $ScriptBlock = {
  5.     param([int]$StartIndex, [int]$EndIndex)
  6.     PowerShell -exec ByPass -f "DownLoadURL.ps1" -StartIndex $StartIndex -EndIndex $EndIndex;
  7. }
  8. $Jobs = @(); $Step = 4;
  9. for ($i = 1; $i -le 100; $i+=$Step) {
  10.     $Start = $i;
  11.     $End = $i + $step - 1;
  12.     $PowerShell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($Start).AddArgument($End);
  13.     $PowerShell.RunspacePool = $Pool;
  14.     $Jobs += $PowerShell.BeginInvoke();
  15. }
  16. while (!$ok) {
  17.     $ok = $True;
  18.     ForEach ($Job In $Jobs) { If (!$Job.IsCompleted) {$ok = $False} }
  19.     sleep -Milliseconds 200
  20. }
复制代码
DownLoadURL.PS1
修改 DownLoadURL.PS1 第 6 行 $type 的值,可以取值从 1~19
  1. param([int]$StartIndex, [int]$EndIndex);
  2. $path = 'E:\52shici\';      #html文件保存路径
  3. $web = New-Object System.Net.WebClient;
  4. $web.Encoding = [Text.Encoding]::UTF8;
  5. $type = 1;
  6. $fd  = $path + $type;
  7. If (!(Test-Path $fd)) { $null = md $fd; }
  8. for ($i=$StartIndex; $i -le $EndIndex; $i++) {
  9.     $url = 'http://www.52shici.com/original.php?type=' + $type + '&page=' + $i;
  10.     $str = $web.DownloadString($url);
  11.     forEach ($m In [regex]::Matches($str, '<li><span class="l"><a\s+target="_blank" href="([^"]*)"')) {
  12.         $var = 'http://www.52shici.com/' + $m.Groups[1].Value;
  13.         $name = ($var -replace '.*\?');
  14.         $s = $web.DownloadString($var);
  15.         $s = $s -replace '(?<=<span class="author">)(.*?)文/', '$1作者 ';
  16.         sc -Path ($fd + '\' + $name + '.html') -Value $s -Enc UTF8;
  17.     }
  18. }
复制代码
1

评分人数

TOP

本帖最后由 WHY 于 2018-11-20 19:29 编辑

下载 http://www.zgshige.com 网页,与楼上一样,也分三个脚本,第1个脚本 startScript.bat 和第2个脚本 startScript.ps1 内容与楼上相同,三个脚本存放在同一目录下。
第 3 个脚本 DownLoadURL.PS1 如下,双击 startScript.bat 运行。
  1. param([int]$StartIndex, [int]$EndIndex);
  2. $path = 'E:\zgshige\';      #html文件保存路径
  3. $web = New-Object System.Net.WebClient;
  4. $web.Encoding = [Text.Encoding]::UTF8;
  5. for ($i=$StartIndex; $i -le $EndIndex; $i++) {
  6.     $url = 'http://www.zgshige.com/zcms/catalog/15112/pc/index_' + $i + '.shtml';
  7.     $str = $web.DownloadString($url);
  8.     forEach ($m In [regex]::Matches($str, '(?i)<a class="fc-green text-uppercase" href="(http[^"]*)"')) {
  9.         $var = $m.Groups[1].Value;
  10.         $fd  = $path + ($var -replace '.*/([^/]+)/[^/]+$', '$1');
  11.         If (!(Test-Path $fd)) { $null = md $fd; }
  12.         $name = $var -replace '.*/([^/]+)\.s?html$', '$1';
  13.         $s = $web.DownloadString($var);
  14.         $s = $s -replace '<h3>(?!TTT)', '$&TTT';
  15.         sc -Path ($fd + '\' + $name + '.html') -Value $s -Enc UTF8;
  16.     }
  17. }
复制代码
改一下正则,提高点速度。
1

评分人数

TOP

本帖最后由 WHY 于 2018-11-26 21:09 编辑

回复 11# lxh623


    假设从 http://www.zgshige.com 下载的 html 存放在 E:\zgshige 目录下。
分两个脚本,zgshige.bat 和 zgshige.PS1,两个脚本存放在同一目录下,双击 bat 文件运行。

zgshige.bat
  1. @echo off
  2. set "htmlPath=E:\zgshige"
  3. dir /b /s "%htmlPath%\*.html" > List.txt
  4. PowerShell -exec ByPass -f "zgshige.PS1" List.txt
  5. del List.txt
  6. pause
复制代码
zgshige.PS1
  1. param([String]$ListFile);
  2. $stream = New-Object IO.StreamWriter('zgshige.Log', $True, [Text.Encoding]::UTF8);
  3. forEach ($file In (type $ListFile -ReadCount 0)) {
  4.     $str = [IO.File]::ReadAllText($file, [Text.Encoding]::UTF8);
  5.     $arr = $str -split '<!--\s*诗歌正文(?:开始|结束)\s*-->';
  6.     If ($arr.Count -eq 3) {
  7.         $s = $arr[1] -replace '<h3>(?!TTT)', '$&TTT';
  8.         $s = $s -replace '(?<!\r)\n', "`r`n";
  9.         $s = $s -replace '&nbsp;|<(?!br)[^>]*>';
  10.         $s = $s -replace '(?m)^\s+';
  11.         $stream.WriteLine($s + "`r`n");
  12.         If (++$n % 6000 -eq 0) { $stream.Flush(); }
  13.     }
  14. }
  15. $stream.Close();
复制代码

TOP

本帖最后由 WHY 于 2018-12-4 23:45 编辑

假设从 http://www.52shici.com 下载的 html 存放在 E:\52shici 目录下。
也是分两个脚本,52shici.bat 和 52shici.PS1,两个脚本存放在同一文件夹下,双击 bat 文件运行。

52shici.bat
  1. @echo off
  2. set "htmlPath=E:\52shici"
  3. dir /b /s "%htmlPath%\*.html" > List.txt
  4. PowerShell -exec ByPass -f "52shici.PS1" List.txt
  5. del List.txt
  6. pause
复制代码
52shici.PS1
  1. param([String]$ListFile);
  2. $stream = New-Object IO.StreamWriter('52shici.Log', $True, [Text.Encoding]::UTF8);
  3. $reg1 = [regex]'<em>([^<>]*)</em>\s*<span class="author">([^<>]*)</span>';
  4. $reg2 = [regex]'<div class="works-content"[\s\S]*被阅读过\s*\d+\s*次';
  5. forEach ($file In (type $ListFile -ReadCount 0)) {
  6.     $str = [IO.File]::ReadAllText($file, [Text.Encoding]::UTF8);
  7.     $m1  = $reg1.Match($str);
  8.     $m2  = $reg2.Match($str);
  9.     If ($m1.Success -and $m2.Success) {
  10.         $s = 'TTT' + $m1.Groups[1].Value + $m1.Groups[2].Value + "`r`n";
  11.         $s += $m2.Groups[0].Value;;
  12.         $s = $s -replace '(?<!\r)\n', '\r\n';
  13.         $s = $s -replace '&nbsp;|<(?!br)[^>]*>';
  14.         $s = $s -replace '(?m)^\s+';
  15.         $stream.WriteLine($s + "`r`n");
  16.         If (++$n % 6000 -eq 0) { $stream.Flush(); }
  17.     }
  18. }
  19. $stream.Close();
复制代码

TOP

回复 21# lxh623


    50 万个文件,每页 60 个文件,就是 8000 多页没下载,可能是你第一次下载 1~100页,第二次下载 200~300页,中间不连续。

以下脚本会把没有下载的存放到 E:\zgshige\Lost 目录,这是第三个脚本 DownLoadURL.PS1,第一、第二个脚本同 6 楼。
(不过这样子折腾很费时间,你看着办)
DownLoadURL.PS1
  1. param([int]$StartIndex, [int]$EndIndex);
  2. $path = 'E:\zgshige\';      #html文件保存路径
  3. $web = New-Object System.Net.WebClient;
  4. $web.Encoding = [Text.Encoding]::UTF8;
  5. for ($i=$StartIndex; $i -le $EndIndex; $i++) {
  6.     $url = 'http://www.zgshige.com/zcms/catalog/15112/pc/index_' + $i + '.shtml';
  7.     $str = $web.DownloadString($url);
  8.     forEach ($m In [regex]::Matches($str, '(?i)<a class="fc-green text-uppercase" href="(http[^"]*)"')) {
  9.         $var = $m.Groups[1].Value;
  10.         $fd  = $var -replace '.*/([^/]+)/[^/]+$', '$1';
  11.         $name = $var -replace '.*/([^/]+)\.s?html$', '$1';
  12.         If (Test-Path ($path + $fd + '\' + $name + '.html')) { continue; }
  13.         $fd = $path + 'Lost\' + $fd;
  14.         If (!(Test-Path $fd)) { $null = md $fd; }
  15.         $s = $web.DownloadString($var);
  16.         $s = $s -replace '<h3>(?!TTT)', '$&TTT';
  17.         sc -Path ($fd + '\' + $name + '.html') -Value $s -Enc UTF8;
  18.     }
  19. }
复制代码
1

评分人数

    • lxh623: 真的太好了!谢谢!技术 + 1

TOP

本帖最后由 WHY 于 2018-12-4 23:58 编辑

回复 29# lxh623


    在我的笔记本上测试 #14 脚本,提取 12000 个文本,3分26秒完成,不算太慢。
大批量文本处理的话,最好做一下磁盘优化(如磁盘碎片整理、安装磁盘驱动)。
有一个小技巧,关闭 Windows Defender 或第三方杀软文件实时监控,可以大幅度提高文件读写速度。
#15脚本少一个引号,已修改。

TOP

回复 34# lxh623


    我觉得没必要再写一个,无非就是怕下载漏了,只要你把下载的页面数字记住就行了。
比如这一次下载 200~300 页,for ($i = 200; $i -le 300; $i+=$Step)
下一次你改成 for ($i = 301; $i -le 400; $i+=$Step) 就会接着下载 301~400 页。

www.zgshige.com 这个网站很怪,前面几百页还好,越往后越慢,点开都难,当然越下载就越慢。

TOP

下载不全的话直接用 #23 的脚本吧,我这里测试 26500~26600 下载时确实很慢,但是不缺页。
不明白你是啥情况。

TOP

本帖最后由 WHY 于 2018-12-13 16:30 编辑

回复 43# lxh623


zgshige.bat
  1. @echo off
  2. echo %time%
  3. set "htmlPath=E:\zgshige"
  4. dir /b /s "%htmlPath%\*.html" > List.txt
  5. PowerShell -exec ByPass -f "zgshige.PS1" List.txt
  6. del List.txt
  7. echo %time%
  8. pause
复制代码
zgshige.ps1
  1. param([String]$ListFile);
  2. $MyPath = $MyInvocation.MyCommand.Path -replace '[^\\]+$';
  3. forEach ($file In (type $ListFile -ReadCount 0)) {
  4.     $str = [IO.File]::ReadAllText($file, [Text.Encoding]::UTF8);
  5.     $arr = $str -split '<div class="(?:text-center b-b b-2x b-lt|col-xs-12 hidden-sm[^"]*)">';
  6.     If ($arr.Count -eq 3) {
  7.         $s = $arr[1] -replace '<h3>(?!TTT)', '$&TTT';         #标题前面加 TTT
  8.         $s = $s -replace '(?<!\r)\n', "`r`n";
  9.         $s = $s -replace '<div class="(signature|m-lg font14|note)">|</?br>|<br/>|</p>', '<br />';
  10.         $s = $s -replace '<!--(?:(?!-->)[\s\S])*-->|&nbsp;';  #删除注释标签和转义空格
  11.         $s = $s -replace '<(?!br|/?h3)[^>]*>';                #保留换行标签和h3标签
  12.         $s = $s -replace '(<br />)+', '$1';
  13.         $s = $s -replace '(?m)^\s+';                          #删除行首空格
  14.         $name = ($file -split '\\')[-2] + '.Log';
  15.         [IO.File]::AppendAllText($MyPath + $name, $s + "`r`n", [Text.Encoding]::UTF8);
  16.     }
  17. }
复制代码

TOP

回复 45# lxh623


    你用的是什么系统?
我改了下,你再试。

TOP

我在 win10,win7测试没有问题。14楼脚本能运行吗?
你看看出错的时候有没有输出 List.txt 文本。

TOP

有些 html 文本不带注释
44楼脚本 zgshige.ps1 第6行改成:
  1. $arr = $str -split '<div class="(?:text-center b-b b-2x b-lt|col-xs-12 hidden-sm[^"]*)">';
复制代码
1

评分人数

TOP

回复 53# lxh623


    顶楼就没说清,不明白你到底需要什么结果。
44楼改了下,试试吧

TOP

本帖最后由 WHY 于 2018-12-17 18:21 编辑

zgshige.bat
  1. @echo off
  2. echo Wait ...
  3. set "htmlPath=E:\zgshige"
  4. dir /b /s "%htmlPath%\*.html" > List.txt
  5. PowerShell -exec ByPass -f "zgshige.PS1" List.txt
  6. del List.txt
  7. echo Done!
  8. pause
复制代码
zgshige.ps1
  1. param([String]$ListFile);
  2. Add-Type -AssemblyName System.Web;
  3. forEach ($file In (type $ListFile -ReadCount 0)) {
  4.     $str = [IO.File]::ReadAllText($file, [Text.Encoding]::UTF8);
  5.     $arr = $str -split '<div (?:class="text-center b-b b-2x b-lt|id="content|class="m-lg font14|class="p-sm)">';
  6.     If ($arr.Count -eq 5) {
  7.         $s = $arr[1] + '<br />' + $arr[2] + '<br />';
  8.         $s += $arr[3] -replace '[\u4E00-\u9FFF\p{P}](?=<(?!/?br)|\r?\n)', '$&<br />';  #中文及标点后面加<br />
  9.         $s = $s -replace '<h3>(?!TTT)', '$&TTT';              #标题前面加 TTT
  10.         $s = $s -replace '<!--(?:(?!-->)[\s\S])*-->';         #删除注释标签
  11.         $s = $s -replace '</?br>|<br/>', '<br />';            #替换<br>、</br>、<br/>
  12.         $s = $s -replace '</?(?!br|h3|[^a-z])[^>]*>', '';     #删除其它标签,保留换行标签和h3标签
  13.         $s =[regex]::Replace($s, '&[^&;]+;',{param($a); [Web.HttpUtility]::HtmlDecode($a.Value)}); #替换html转义字符
  14.         $s = $s -replace '^\s+';
  15.         $s = $s -replace '(?:<br />\s*){2,}', '<br /><br />';
  16.         $s = $s -replace '\s+', ' ';
  17.         $name = ($file -split '\\')[-2] + '.Log';
  18.         [IO.File]::AppendAllText($name, $s + "`r`n", [Text.Encoding]::UTF8);
  19.     }
  20. }
复制代码

TOP

回复  WHY



有些倒退。分节不见了。哎。确实复杂。
lxh623 发表于 2018-12-14 15:07



    什么呀,44楼就是这样子的,为啥不一开始就说清楚啊?我至今任然搞不懂你到底需要什么样的格式。

TOP

返回列表