主頁 > 知識庫 > asp.net采集網(wǎng)頁圖片的具體方法

asp.net采集網(wǎng)頁圖片的具體方法

熱門標簽:網(wǎng)站排名優(yōu)化 客戶服務 科大訊飛語音識別系統(tǒng) 電商新玩法 人工智能 百度AI接口 國美全國運營中心 電銷業(yè)務
在網(wǎng)上找了下大多都是通過字符串操作找出img標簽,這種方式操作起來比較麻煩,而且代碼看起來比較累。這里我用的方法是通過WebBrowser來加載一個頁面,然后HTMLDocument類來操作省去了字符串操作的步驟,直接調用GetElementsByTagName把所有圖片地址返回到一個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");
                //調用保存遠程圖片函數(shù)
                SaveImageFromWeb(sImgUrl, this.savePath);
            }
        }
        //保存遠程圖片函數(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;
            }
        }
    }
}
//-----------------調用代碼--------------------
GatherPic gatherpic = new GatherPic(“http://www.baidu.com”,"C:\test");
//請確保c:\下存在test路徑
gatherpic.start()
您可能感興趣的文章:
  • 利用MSXML2.XmlHttp和Adodb.Stream采集圖片
  • asp.net(c#)做一個網(wǎng)頁數(shù)據(jù)采集工具
  • asp.net c#采集需要登錄頁面的實現(xiàn)原理及代碼
  • PHP遠程采集圖片詳細教程
  • asp.net采集頁面上所有圖像圖片資源的具體方法

標簽:益陽 咸寧 拉薩 南平 攀枝花 廈門 棗莊 POS機

巨人網(wǎng)絡通訊聲明:本文標題《asp.net采集網(wǎng)頁圖片的具體方法》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266