資源簡介
支持爬取圖片,一個完整的爬蟲示例,希望可以幫到大家

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
using?System.Web;
using?System.Net;
using?System.IO;
using?System.Text.Regularexpressions;
using?System.Threading;
namespace?wp
{
????public?delegate?void?GetHttpTextDelegate(object?baseUrll);
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????????
????????}
????????Dictionary?unload?=?new?Dictionary();
????????Dictionary?loaded?=?new?Dictionary();
????????List?imageUrl?=?new?List();
????????private?Thread?th;
????????string?baseUrl?=?string.Empty;
????????///?
????????///?開?始
????????///?
????????///?
????????///?
????????private?void?btnBegin_Click(object?sender?EventArgs?e)
????????{
????????????if(string.IsNullOrEmpty(txtUrl.Text))
????????????{
????????????????return;
????????????}
????????????if(th!=null&&th.ThreadState==ThreadState.Suspended)
????????????{
????????????????try
????????????????{
????????????????????if?(baseUrl?==?txtUrl.Text.Trim())
????????????????????????th.Resume();
????????????????????else
????????????????????????th.Abort();
????????????????????btnBegin.Enabled?=?false;
????????????????????btnStop.Enabled?=?true;
????????????????}
????????????????catch?(Exception)
????????????????{
????????????????}
????????????????return;
????????????}
????????????loaded?=?new?Dictionary();
????????????unload?=?new?Dictionary();
????????????//unload.Add(“http://news.sina.com.cn/“?0);
????????????//string??baseUrl?=?“news.sina.com.cn“;
????????????unload.Add(“http://“?+?txtUrl.Text.Trim()?0);
????????????baseUrl?=?txtUrl.Text.Trim();
????????????th?=?new?Thread(new?ParameterizedThreadStart(GetHttpInfo));
????????????th.Start(baseUrl);
????????????btnBegin.Enabled?=?false;
????????????btnStop.Enabled?=?true;
????????}
????????private?void?GetHttpInfo(object?baseUrl)
????????{
????????????//GetHttpTextDelegate?gt?=?new?GetHttpTextDelegate(GetHttpText);
????????????//this.Invoke(gtnew?object[]{baseUrl});
????????????GetHttpText(baseUrl);
????????}
????????private?void?GetHttpText(object?baseUrl)
????????{
????????????while?(true)
????????????{
????????????????if(unload==null||unload.Keys.Count<=0)
????????????????{
????????????????????return;
????????????????}
????????????????string?url?=?unload.First().Key;
????????????????int?depth?=?unload.First().Value;
????????????????loaded.Add(url?depth);
????????????????unload.Remove(url);
????????????????////創(chuàng)建http請求??獲取http參數(shù)
????????????????HttpWebRequest?req?=?(HttpWebRequest)WebRequest.Create(url);
????????????????////請求方式
????????????????req.Method
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????187??2017-05-03?14:09??PC\PC\App.config
?????文件??????15360??2017-05-03?14:15??PC\PC\bin\Debug\PC.exe
?????文件????????187??2017-05-03?14:09??PC\PC\bin\Debug\PC.exe.config
?????文件??????30208??2017-05-03?14:15??PC\PC\bin\Debug\PC.pdb
?????文件??????24224??2017-05-03?14:16??PC\PC\bin\Debug\PC.vshost.exe
?????文件????????187??2017-05-03?14:09??PC\PC\bin\Debug\PC.vshost.exe.config
?????文件????????490??2010-03-17?22:39??PC\PC\bin\Debug\PC.vshost.exe.manifest
?????文件??????10016??2017-05-03?14:15??PC\PC\Form1.cs
?????文件???????6761??2017-05-03?14:09??PC\PC\Form1.Designer.cs
?????文件???????5817??2017-05-03?14:09??PC\PC\Form1.resx
?????文件???????1453??2017-05-03?14:09??PC\PC\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7684??2017-05-03?14:09??PC\PC\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????456??2017-05-03?14:16??PC\PC\obj\Debug\PC.csproj.FileListAbsolute.txt
?????文件????????977??2017-05-03?14:09??PC\PC\obj\Debug\PC.csproj.GenerateResource.Cache
?????文件???????2211??2017-05-03?14:09??PC\PC\obj\Debug\PC.csprojResolveAssemblyReference.cache
?????文件??????15360??2017-05-03?14:15??PC\PC\obj\Debug\PC.exe
?????文件??????30208??2017-05-03?14:15??PC\PC\obj\Debug\PC.pdb
?????文件????????180??2017-05-03?14:09??PC\PC\obj\Debug\PC.Properties.Resources.resources
?????文件??????????0??2017-05-03?14:09??PC\PC\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2017-05-03?14:09??PC\PC\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2017-05-03?14:09??PC\PC\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件???????5125??2017-05-03?14:09??PC\PC\PC.csproj
?????文件????????528??2017-05-03?14:09??PC\PC\Program.cs
?????文件???????1346??2017-05-03?14:09??PC\PC\Properties\AssemblyInfo.cs
?????文件???????2856??2017-05-03?14:09??PC\PC\Properties\Resources.Designer.cs
?????文件???????5612??2017-05-03?14:09??PC\PC\Properties\Resources.resx
?????文件???????1089??2017-05-03?14:09??PC\PC\Properties\Settings.Designer.cs
?????文件????????249??2017-05-03?14:09??PC\PC\Properties\Settings.settings
?????文件???????3784??2017-05-03?14:09??PC\PC\wp.csproj
?????文件????????975??2017-05-03?14:08??PC\PC.sln
............此處省略12個文件信息
評論
共有 條評論