資源簡介
多線程爬取網絡圖片,直接可以運行!

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.IO;
using?System.Linq;
using?System.Net;
using?System.Text;
using?System.Text.Regularexpressions;
using?System.Threading;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?網絡爬蟲
{
???public?class?DownLoad
????{
???????private?string?_rootUrl?=?null;//存放根URL,所輸入的鏈接
???????private?string?_baseUrl?=?null;//存放處理后的根URL,所輸入的鏈接
???????private?string?_path?=?null;//存放所選擇保存圖片的路徑
???????private?bool?_stop?=?true;//判斷是否點擊停止下載
???????private?System.Threading.Timer?_checkTimer?=?null;//一個時間控制器,也是一個線程
???????private?readonly?object?_locker?=?new?object();//排他鎖,保證請求的完整性
???????private?int?_maxTime?=?2?*?60?*?1000;//請求的最大時間
???????private?static?Encoding?GB18030?=?Encoding.GetEncoding(“GB18030“);//編碼格式,GB18030兼容GBK和GB2312
???????private?Encoding?_encoding?=?GB18030;
???????private?int?_index?=?0;//索引標志,用于判斷所下載的圖片數量
???????private?bool[]?_reqsBusy?=?null;//判斷是否是正在工作,true為正在工作
???????private?int?_reqCount?=?4;//初始值,默認是四個線程工作
???????private?WorkingUnitCollection?_workingSignals;//類的對象,用于判斷是否工作
???????private?Dictionary?_urlsLoaded?=?new?Dictionary();//數據字典,用于存放未下載的鏈接
???????private?Dictionary?_urlsUnload?=?new?Dictionary();//數據字典,用于存放已經下載的鏈接
???????public?delegate?void?ContentsSavedHandler(string?path?string?url);//定義一個委托
???????public?event?ContentsSavedHandler?ContentsSaved?=?null;//定義一個事件,用于保存內容時觸發
???????public?delegate?void?DownloadFinishHandler(int?count);
???????public?event?DownloadFinishHandler?DownloadFinish?=?null;//定義一個事件,用于完成下載時觸發
???????///?
???????///?定義一個類,初始化請求時所需要的屬性
???????///?
???????private?class?RequestState
???????{
???????????///?
???????????///?接收數據包的空間大小
???????????///?
???????????private?const?int?BUFFER_SIZE?=?131072;
???????????///?
???????????///?接收數據包的buffer
???????????///?
???????????private?byte[]?_data?=?new?byte[BUFFER_SIZE];
???????????///?
???????????///?存放所有接收到的字符
???????????///?
???????????private?StringBuilder?_sb?=?new?StringBuilder();
???????????///?
???????????///?請求
???????????///?
???????????public?HttpWebRequest?Req?{?get;?private?set;?}
???????????///?
???????????///?請求的URL
???????????///?
???????????public?string?Url?{?get;?private?set;?}
???????????///?
???????????///?此次請求的相對深度
???????????///?
???????????public?int?Depth?{?get;?private?set;?}
???????????///?
???????????///?工作實例的編號
???????????///?
???????????public?int?Index?{?get;?private?set;?}
???????????///?
???????????///?接收數據流
???????????///?
???????????public?Stream?ResStream?{?get;?set;?}
???????????public?StringBuilder?Html
???????????{
???????????????get
???????????????{
???????????????????return?_sb;
???????????????}
???????????}
???????????public?byte[]?Data
???????????{
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????187??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\App.config
?????文件??????16361??2016-08-17?13:53??網絡爬蟲\網絡爬蟲\bin\...-1.png
?????文件?????588427??2016-08-17?13:56??網絡爬蟲\網絡爬蟲\bin\0.txt
?????文件??????20400??2016-08-17?13:53??網絡爬蟲\網絡爬蟲\bin\1.png
?????文件??????23552??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.exe
?????文件????????187??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.exe.config
?????文件??????46592??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.pdb
?????文件??????22984??2016-09-30?10:21??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.vshost.exe
?????文件????????187??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.vshost.exe.config
?????文件????????490??2012-06-06?02:06??網絡爬蟲\網絡爬蟲\bin\Debug\網絡爬蟲.vshost.exe.manifest
?????文件??????19803??2016-09-29?19:15??網絡爬蟲\網絡爬蟲\DownLoad.cs
?????文件???????3139??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\Form1.cs
?????文件???????6406??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\Form1.Designer.cs
?????文件???????7267??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\Form1.resx
?????文件???????1278??2016-08-26?12:24??網絡爬蟲\網絡爬蟲\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6891??2016-08-18?19:15??網絡爬蟲\網絡爬蟲\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件????????744??2016-09-30?10:21??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.csproj.FileListAbsolute.txt
?????文件????????975??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.csproj.GenerateResource.Cache
?????文件???????2143??2016-08-17?12:04??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.csprojResolveAssemblyReference.cache
?????文件??????23552??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.exe
?????文件???????1332??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.Form1.resources
?????文件??????46592??2016-09-30?10:20??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.pdb
?????文件????????180??2016-08-17?13:53??網絡爬蟲\網絡爬蟲\obj\Debug\網絡爬蟲.Properties.Resources.resources
?????文件????????524??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\Program.cs
?????文件???????1344??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\Properties\AssemblyInfo.cs
?????文件???????2876??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\Properties\Resources.Designer.cs
?????文件???????5612??2016-08-17?10:59??網絡爬蟲\網絡爬蟲\Properties\Resources.resx
............此處省略16個文件信息
評論
共有 條評論