公交车上荫蒂添的好舒服的电影-公用玩物(np双xing总受)-公用小荡货芊芊-公与妇仑乱hd-攻把受做哭边走边肉楼梯play-古装一级淫片a免费播放口

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

C# 抓取網頁img的src帶參數的圖片鏈接,并下載

admin
2021年3月18日 12:8 本文熱度 2943
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
 
namespace ImageCollection
{
    public partial class Form1 : Form
    {
        private static string Path = AppDomain.CurrentDomain.BaseDirectory + "img";
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnshuaqu_Click(object sender, EventArgs e)
        {
            string url = txturl.Text.Trim();
            if (string.IsNullOrEmpty(url))
            {
                MessageBox.Show("請輸入URl");
                return;
            }
            txtimg.AppendText("開始抓取中:\r\n");
            Thread th = new Thread(() => ShuaQu(url)) { IsBackground = true };
            th.Start();
        }
 
        private void ShuaQu(string url)
        {
            DirectoryInfo di = new DirectoryInfo(Path);
            if (System.IO.Directory.Exists(Path))
            {
                di.Delete(true);
            }
            System.IO.Directory.CreateDirectory(Path);
            string result = WebHttp.HttpGet(url, null, 3);
            string[] str = GetHtmlImageUrlList(result);
            txtimg.Invoke(new Action(() =>
            {
                txtimg.AppendText("已經獲取到數據!"+str.Count() + "\r\n");
            }));
            //建立獲取網頁標題正則表達式  
            String regex = @"<title>.+</title>";
 
            //返回網頁標題  
            String title = Regex.Match(result, regex).ToString();
            txttitle.Invoke(new Action(() => {
                txttitle.Text = Regex.Replace(title, @"[\""]+", ""); 
            }));
            foreach (string s in str)
            {
                Uri u = new Uri(s);
                if (u.Host == "www.xxx.com")
                {
                    Thread downimg = new Thread(() => Get_img(s)) { IsBackground = true };
                    downimg.Start();
                    txtimg.Invoke(new Action(() => {
                        txtimg.AppendText(s + "\r\n");
                    }));
                } 
            }
            txtimg.Invoke(new Action(() =>
            {
                txtimg.AppendText("全部抓取完成!\r\n");
            }));
        }
 
        public void Get_img(string imgpath)
        {
            
 
            string[] file = imgpath.Split('?');
            string name = System.IO.Path.GetFileName(file[0]);
            WebClient mywebclient = new WebClient();
            mywebclient.DownloadFile(imgpath, Path + @"\" + name);
            //Bitmap img = null;
            //HttpWebRequest req;
            //HttpWebResponse res = null;
            //try
            //{
            //    System.Uri httpUrl = new System.Uri(imgpath);
            //    req = (HttpWebRequest)(WebRequest.Create(httpUrl));
            //    req.Timeout = 180000; //設置超時值10
            //    req.UserAgent = "XXXXX";
            //    req.Accept = "XXXXXX";
            //    req.Method = "GET";
            //    res = (HttpWebResponse)(req.GetResponse());
            //    img = new Bitmap(res.GetResponseStream());//獲取圖片流                
            //    img.Save(Path + @"\"+name);//隨機名
            //}
 
            //catch (Exception ex)
            //{
            //    string aa = ex.Message;
            //}
            //finally
            //{
            //    res.Close();
            //}
        }
 
 
        /// <summary> 
        /// 取得HTML中所有圖片的 URL 
        /// </summary> 
        /// <param name="sHtmlText">HTML代碼</param> 
        /// <returns>圖片的URL列表</returns> 
        private string[] GetHtmlImageUrlList(string sHtmlText)
        {
            // 定義正則表達式用來匹配 img 標簽 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
 
            // 搜索匹配的字符串 
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            string[] sUrlList = new string[matches.Count];
 
            // 取得匹配項列表 
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;
            return sUrlList;
        }
    }
}



#region 下載圖片到Image
public static Image UrlToImage(string url) {
    WebClient mywebclient = new WebClient();
    byte[] Bytes = mywebclient.DownloadData(url);
    using (MemoryStream ms = new MemoryStream(Bytes)) {
        Image outputImg = Image.FromStream(ms);
        return outputImg;
    }
}
#endregion


該文章在 2021/3/18 12:09:33 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产高清无码视频专区 | 国产一区私人高清影院 | 国外欧美一区另类中文字幕 | 国产成人精品免费 | 国产麻豆精品av在线观看 | 极品嫩模众筹福利写真视频 | 97人妻人人揉人人躁人人 | 国产探花在线精品一区二 | 国产麻豆精品av在线观看 | 国产欧美日韩亚洲精品区23 | av免费无码在线 | av色综合久久天堂av色综合 | 国产美女大量吞 | 2025年国产精品每日更新 | 国产高清免费av片在线观看不卡 | 国产精品一区91 | 成人在线午夜影院 | 国产精品盗摄!偷窥盗 | 动漫福利精品一 | 精品无码v免费 | 91久久夜色精品国产伊甸园 | 国产ts人妖在线视频网站 | 国产av无码专区亚洲av | 国产无码综合一区二区 | 国产三级久久久精 | 国产一区二区草草影 | 国产内射老熟女aaaa | 国产精品免费无遮挡永久视频 | 国产精品亚洲自在线播放页码 | 国产精品毛片一区二区 | 国产精品成人一区二区 | 国产在线麻豆 | 国产91精品看黄网站在线观看 | 精品无码理论片 | 国产野战无套av毛片 | 国产亚洲成a人片在线观看 国产亚洲成v人片在线观黄桃 | 国产精品高潮69情节跌宕起伏 | 99久久久无码国产精品免费了 | 精品日韩欧美亚洲 | 精品亚洲欧美v国产一区二区三区 | 成人午夜视频在线观 |