asp.net采集網(wǎng)頁圖片的具體方法
更新時(shí)間:2013年06月12日 14:15:44 作者:
采集網(wǎng)頁上圖片的主要關(guān)鍵是在怎么解析出頁面代碼里那些img標(biāo)簽的src屬性
在網(wǎng)上找了下大多都是通過字符串操作找出img標(biāo)簽,這種方式操作起來比較麻煩,而且代碼看起來比較累。這里我用的方法是通過WebBrowser來加載一個(gè)頁面,然后HTMLDocument類來操作省去了字符串操作的步驟,直接調(diào)用GetElementsByTagName把所有圖片地址返回到一個(gè)HtmlElementCollection對象里。
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class GatherPic
{
private string savePath;
private string getUrl;
private WebBrowser wb;
private int iImgCount;
//初始化參數(shù)
public GatherPic(string sWebUrl, string sSavePath)
{
this.getUrl = sWebUrl;
this.savePath = sSavePath;
}
//開始采集
public bool start()
{
if (getUrl.Trim().Equals(""))
{
MessageBox.Show("哪來的蝦米連網(wǎng)址都沒輸!");
return false;
}
this.wb = new WebBrowser();
this.wb.Navigate(getUrl);
//委托事件
this.wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
return true;
}
//WebBrowser.DocumentCompleted委托事件
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//頁面里框架iframe加載完成不掉用SearchImgList()
if (e.Url != wb.Document.Url) return;
SearchImgList();
}
//檢查出所有圖片并采集到本地
public void SearchImgList()
{
string sImgUrl;
//取得所有圖片地址
HtmlElementCollection elemColl = this.wb.Document.GetElementsByTagName("img");
this.iImgCount = elemColl.Count;
foreach (HtmlElement elem in elemColl)
{
sImgUrl = elem.GetAttribute("src");
//調(diào)用保存遠(yuǎn)程圖片函數(shù)
SaveImageFromWeb(sImgUrl, this.savePath);
}
}
//保存遠(yuǎn)程圖片函數(shù)
public int SaveImageFromWeb(string imgUrl, string path)
{
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/") + 1);
path = path + "\\" + imgName;
string defaultType = ".jpg";
string[] imgTypes = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
foreach (string it in imgTypes)
{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(".bmp"))
imgType = defaultType;
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
request.Timeout = 10000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if (response.ContentType.ToLower().StartsWith("image/"))
{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
// CreateDirectory(path);
FileStream fso = new FileStream(path, FileMode.Create);
while (l < imgLong)
{
int i = stream.Read(arrayByte, 0, 1024);
fso.Write(arrayByte, 0, i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
{
return 0;
}
}
catch (WebException)
{
return 0;
}
catch (UriFormatException)
{
return 0;
}
}
}
}
//-----------------調(diào)用代碼--------------------
GatherPic gatherpic = new GatherPic(“http://www.baidu.com”,"C:\test");
//請確保c:\下存在test路徑
gatherpic.start()
代碼如下:
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class GatherPic
{
private string savePath;
private string getUrl;
private WebBrowser wb;
private int iImgCount;
//初始化參數(shù)
public GatherPic(string sWebUrl, string sSavePath)
{
this.getUrl = sWebUrl;
this.savePath = sSavePath;
}
//開始采集
public bool start()
{
if (getUrl.Trim().Equals(""))
{
MessageBox.Show("哪來的蝦米連網(wǎng)址都沒輸!");
return false;
}
this.wb = new WebBrowser();
this.wb.Navigate(getUrl);
//委托事件
this.wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
return true;
}
//WebBrowser.DocumentCompleted委托事件
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//頁面里框架iframe加載完成不掉用SearchImgList()
if (e.Url != wb.Document.Url) return;
SearchImgList();
}
//檢查出所有圖片并采集到本地
public void SearchImgList()
{
string sImgUrl;
//取得所有圖片地址
HtmlElementCollection elemColl = this.wb.Document.GetElementsByTagName("img");
this.iImgCount = elemColl.Count;
foreach (HtmlElement elem in elemColl)
{
sImgUrl = elem.GetAttribute("src");
//調(diào)用保存遠(yuǎn)程圖片函數(shù)
SaveImageFromWeb(sImgUrl, this.savePath);
}
}
//保存遠(yuǎn)程圖片函數(shù)
public int SaveImageFromWeb(string imgUrl, string path)
{
string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/") + 1);
path = path + "\\" + imgName;
string defaultType = ".jpg";
string[] imgTypes = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
foreach (string it in imgTypes)
{
if (imgType.ToLower().Equals(it))
break;
if (it.Equals(".bmp"))
imgType = defaultType;
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
request.Timeout = 10000;
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
if (response.ContentType.ToLower().StartsWith("image/"))
{
byte[] arrayByte = new byte[1024];
int imgLong = (int)response.ContentLength;
int l = 0;
// CreateDirectory(path);
FileStream fso = new FileStream(path, FileMode.Create);
while (l < imgLong)
{
int i = stream.Read(arrayByte, 0, 1024);
fso.Write(arrayByte, 0, i);
l += i;
}
fso.Close();
stream.Close();
response.Close();
return 1;
}
else
{
return 0;
}
}
catch (WebException)
{
return 0;
}
catch (UriFormatException)
{
return 0;
}
}
}
}
//-----------------調(diào)用代碼--------------------
GatherPic gatherpic = new GatherPic(“http://www.baidu.com”,"C:\test");
//請確保c:\下存在test路徑
gatherpic.start()
相關(guān)文章
Linux服務(wù)器下利用Docker部署.net Core項(xiàng)目的全過程
這篇文章主要給大家介紹了關(guān)于在Linux服務(wù)器下利用Docker部署.net Core項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用.net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07使用ajax局部刷新gridview進(jìn)行數(shù)據(jù)綁定示例
很多用戶都有這樣需求,比如:點(diǎn)擊按鈕,刷新 GridView 中的數(shù)據(jù),而不是這個(gè)頁面刷新。使用簡單的 XMLHttpRequest就可以直接實(shí)現(xiàn)2014-02-02asp.net?core?中的Jwt(Json?Web?Token)的使用詳解
session不支持分布式并且在服務(wù)器存儲(chǔ)一份用戶登錄的信息,這份登錄信息會(huì)在響應(yīng)時(shí)傳遞給瀏覽器,告訴其保存為cookie,以便下次請求時(shí)發(fā)送給我們的應(yīng)用,這篇文章主要介紹了asp.net?core?中的Jwt(Json?Web?Token)的使用,需要的朋友可以參考下2022-10-10asp.net后臺(tái)如何輸出js腳本使用什么方法可以實(shí)現(xiàn)
asp.net后臺(tái)如何輸出js腳本,用page.ClientScript.RegisterStartupScript方式實(shí)現(xiàn),實(shí)現(xiàn)示例如下,感興趣的朋友不要錯(cuò)過2014-01-01