找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 17002|回复: 0

[原创代码] powershell 网页爬虫获取时光网电影数据库续(并行处理)

[复制链接]
发表于 2018-2-1 12:26:39 | 显示全部楼层 |阅读模式
前一篇讲到采用并行处理提高速度, 这个时候需要给前面的脚本传递参数, 而且要在前面的那个脚本《000___mtime_抓取电影网页-选数据-逗号分列.ps1》的第一行加入以下代码:
  1. param([int]$start_id_txt_param=$(throw "Parameter missing: -start_id_txt_param 3042"),   [int]$end_id_txt_param=$(throw "Parameter missing: -end_id_txt_param 3242"),     [int]$call_cnt=$(throw "Parameter missing: -call_cnt 132")      )
  2. $USING_PARAM_ENABLE=1
复制代码
将下面的代码保存为 《并行.ps1 》, 然后同样需要设置如下参数:
$start_index = 12500  # MAX mtime id now is 239000. 开始处理的文件 id
$step        = 5000                                                      并行处理的文件个数
$repeat_cnt  = 5                                                        需要启动多少个任务,这里是5 个
$end_index   = 240000 # MAX mtime id now is 239000.

  1. $throttleLimit = 5
  2. $iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
  3. $Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $iss, $Host)
  4. $Pool.Open()


  5. $log_file="D:\迅雷下载\fork.log"


  6.   if((Test-Path $log_file))  {
  7.     Remove-Item $log_file
  8.   }


  9. $ScriptBlock = {
  10.     param($s,$e,$x)
  11.     #Start-Sleep -Seconds 2
  12.         #[System.Console]::WriteLine("Processing XXX.ps1 -start_id_txt_param $s -end_id_txt_param $e -call_cnt $x")
  13.    
  14.         D:\迅雷下载\000___mtime_抓取电影网页-选数据-逗号分列.ps1 -start_id_txt_param $s -end_id_txt_param $e -call_cnt $x
  15.    
  16. }


  17. $start_index = 12500  # MAX mtime id now is 239000.
  18. $step        = 5000
  19. $repeat_cnt  = 5
  20. $end_index   = 240000 # MAX mtime id now is 239000.
  21. if($start_index+$step *$repeat_cnt  -gt $end_index){
  22.   write-host "Error:" -ForegroundColor Red
  23.   write-host "Max mtime for now is 24000, you are using a number greater than 24000!!!" -ForegroundColor Red
  24.   cmd /c "pause"
  25.   exit
  26. }
  27. for ($x = 1; $x -le $repeat_cnt; $x++) {
  28.     $start=$start_index+($x-1)*$step
  29.         $end=$start+$step
  30.         write-host "Processing XXX.ps1 -start_id_txt_param $start -end_id_txt_param $end -call_cnt $x"  
  31.          "Processing XXX.ps1 -start_id_txt_param $start -end_id_txt_param $end -call_cnt $x" |Out-File -Append $log_file
  32.     $powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($start).AddArgument($end).AddArgument($x)
  33.     $powershell.RunspacePool = $Pool
  34.     $handle = $powershell.BeginInvoke()
  35.         #cmd /c "pause"
  36. }
  37. $handle.IsCompleted
  38. cmd /c "pause"
复制代码

评分

参与人数 1技术 +1 收起 理由
老刘1号 + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 03:10 , Processed in 0.025755 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表