<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        asp.net(c#)做一個網頁數據采集工具

        來源:懂視網 責編:小采 時間:2020-11-27 22:43:37
        文檔

        asp.net(c#)做一個網頁數據采集工具

        asp.net(c#)做一個網頁數據采集工具:通過這個軟件一兩天就完成了幾千產品數據的錄入,可見很多工作不是一味用人工去做,作為一個程序員,就是要讓很多讓那些經常做重復性的、繁瑣的工作中的人解放出來。下面只是寫了一些核心代碼,而且采集必須要和對應網站相掛鉤,作者:鄭少群 代碼如下://提取
        推薦度:
        導讀asp.net(c#)做一個網頁數據采集工具:通過這個軟件一兩天就完成了幾千產品數據的錄入,可見很多工作不是一味用人工去做,作為一個程序員,就是要讓很多讓那些經常做重復性的、繁瑣的工作中的人解放出來。下面只是寫了一些核心代碼,而且采集必須要和對應網站相掛鉤,作者:鄭少群 代碼如下://提取

        通過這個軟件一兩天就完成了幾千產品數據的錄入,可見很多工作不是一味用人工去做,作為一個程序員,就是要讓很多讓那些經常做重復性的、繁瑣的工作中的人解放出來。下面只是寫了一些核心代碼,而且采集必須要和對應網站相掛鉤,作者:鄭少群

        代碼如下:


        //提取產品列表頁中產品最終頁的網頁
        private void button1_Click(object sender, EventArgs e)
        {
        if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
        {
        MessageBox.Show("網址和域名不能為空!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
        }
        try
        {
        string Html = inc.GetHtml("http://study.pctoday.net.cn");
        //ArrayList al = inc.GetMatchesStr(Html, "<a[^>]*?>.*?</a>");
        ArrayList al = inc.GetMatchesStr(Html, @"href\s*=\s*(?:[\'\""\s](?<1>[^\""\']*)[\'\""])");//提取鏈接


        " title="Replica Watches:">Replica Watches Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches StoreExclusive Replica Rolex Watches, Tag Heuer Watches Replica, Cartier Watches online Sale!
        StringBuilder sb = new StringBuilder();
        foreach (object var in al)
        {
        string a = var.ToString().Replace("\"", "").Replace("'", "");
        a = Regex.Replace(a, "href=", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        if (a.StartsWith("/"))
        a = textBox2.Text.Trim() + a;
        if (!a.StartsWith("http://"))
        a = "http://" + a;
        sb.Append(a + "\r\n");
        }
        textBox5.Text = sb.ToString();//把提取到網址

        輸出到一個textBox,每個鏈接占一行

        MessageBox.Show("共提取" + al.Count.ToString() + "個鏈接", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
        catch (Exception err)
        {
        MessageBox.Show("提取出錯!原因:" + err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        }


        //把采集的產品頁面html代碼進行字符串處理,提取需要的代碼,最后保存到本地一個access數據庫中,同時提取產品圖片地址并自動現在圖片到本地images文件夾下

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
        //填充產品表
        Database.ExecuteNonQuery("delete from Tb_Product");
        DataTable dt2 = new DataTable();
        OleDbConnection conn = new OleDbConnection(Database.ConnectionStrings);
        OleDbDataAdapter da = new OleDbDataAdapter("select * from Tb_Product", conn);
        OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
        da.Fill(dt2);
        dt2.Rows.Clear();

        BackgroundWorker worker = (BackgroundWorker)sender;//這個是做一個進度條

        string[] Urls = textBox5.Text.Trim().ToLower().Replace("\r\n", ",").Split(',');
        DataTable dt = new DataTable();
        StringBuilder ErrorStr = new StringBuilder();
        string html = "", ImageDir = AppDomain.CurrentDomain.BaseDirectory + "Images\\";

        //循環每次采集網址
        for (int i = 0; i < Urls.Length; i++)
        {
        try
        {
        if (!worker.CancellationPending)
        {
        if (Urls[i] == "")
        return;
        html = inc.GetHtml(Urls[i]);//獲取該url的html代碼
        DataRow NewRow = dt2.NewRow();

        //產品名
        string ProductName = html.Substring(html.IndexOf("<title>") + 7);
        NewRow["ProductName"] = ProductName.Remove(ProductName.IndexOf("</title>")).Trim();

        //產品編號
        NewRow["ModelId"] = NewRow["ProductName"].ToString().Substring(NewRow["ProductName"].ToString().IndexOf("Model:") + 6).Trim();

        //產品介紹,這些都是根據不同網站的html做相應的修改
        string Introduce = html.Substring(html.IndexOf("Product Details") + 26);
        Introduce = Introduce.Remove(Introduce.IndexOf("</table>") + 8).Trim()

        NewRow["Introduce"] = Introduce;

        " title="Replica Watches:">Replica Watches Buy Full Quality Popular Luxury Watches at Amazing Price, Your One Stop Discount Swiss Watches StoreExclusive Replica Rolex Watches, Tag Heuer Watches Replica, Cartier Watches online Sale!
        //下載圖片
        string ProductImage = html.Substring(html.IndexOf("align=center><img") + 17);
        ProductImage = textBox2.Text.Trim() + ProductImage.Substring(ProductImage.IndexOf("src=\"") + 5);
        ProductImage = ProductImage.Remove(ProductImage.IndexOf("\""));
        try
        {
        inc.DownFile(ProductImage, ImageDir + ProductImage.Substring(ProductImage.LastIndexOf("/") + 1));
        }
        catch (Exception)
        {
        ErrorStr.Append("下載圖片失敗,圖片地址:" + ImageDir + ProductImage.Substring(ProductImage.LastIndexOf("/") + 1) + "\r\n");
        }


        dt2.Rows.Add(NewRow);

        //Thread.Sleep(100);
        worker.ReportProgress((i + 1) * 100 / Urls.Length, i);
        toolStripStatusLabel1.Text = "處理進度:" + (i + 1).ToString() + "/" + Urls.Length.ToString();//進度條
        }

        }
        catch (Exception err)
        {
        ErrorStr.Append("采集錯誤:" + err.Message + ";網址:" + Urls[i] + "\r\n");
        }
        }
        da.Update(dt2);
        DataBind(dt2);
        ShowError(ErrorStr.ToString());
        }

        /// <summary>
        /// ASPX頁面生成靜態Html頁面,作者:鄭少群
        /// </summary>
        public static string GetHtml(string url)
        {
        StreamReader sr = null;
        string str = null;
        //讀取遠程路徑
        WebRequest request = WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
        str = sr.ReadToEnd();
        sr.Close();
        return str;
        }


        // 提取HTML代碼中的網址
        public static ArrayList GetMatchesStr(string htmlCode, string strRegex)
        {
        ArrayList al = new ArrayList();

        Regex r = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
        MatchCollection m = r.Matches(htmlCode);

        for (int i = 0; i < m.Count; i++)
        {
        bool rep = false;
        string strNew = m[i].ToString();

        // 過濾重復的URL
        foreach (string str in al)
        {
        if (strNew == str)
        {
        rep = true;
        break;
        }
        }

        if (!rep) al.Add(strNew);
        }

        al.Sort();

        return al;
        }

        public static void DownFile(string Url, string Path)
        {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        long size = response.ContentLength;
        //創建文件流對象
        using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write))
        {
        byte[] b = new byte[1025];
        int n = 0;
        while ((n = stream.Read(b, 0, 1024)) > 0)
        {
        fs.Write(b, 0, n);
        }
        }
        }

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        asp.net(c#)做一個網頁數據采集工具

        asp.net(c#)做一個網頁數據采集工具:通過這個軟件一兩天就完成了幾千產品數據的錄入,可見很多工作不是一味用人工去做,作為一個程序員,就是要讓很多讓那些經常做重復性的、繁瑣的工作中的人解放出來。下面只是寫了一些核心代碼,而且采集必須要和對應網站相掛鉤,作者:鄭少群 代碼如下://提取
        推薦度:
        標簽: 做一個 c# ASP.NET
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: h在线看免费视频网站男男| 亚洲欧洲日韩极速播放| 一区免费在线观看| 亚洲av片一区二区三区| 免费在线人人电影网| 亚洲AV日韩精品一区二区三区| 亚洲av无码专区在线观看亚| 国产免费黄色大片| 三级片免费观看久久| 亚洲成AⅤ人影院在线观看| 成人特级毛片69免费观看| 亚洲熟妇无码另类久久久| 在线看片免费人成视频福利| 亚洲欧洲日产国码av系列天堂 | 99在线免费视频| 亚洲bt加勒比一区二区| 亚洲毛片免费视频| 亚洲精品精华液一区二区| 免费一级特黄特色大片在线| 国产男女爽爽爽免费视频| 亚洲av日韩av天堂影片精品| 国产成人免费在线| 国产精品亚洲专一区二区三区| 亚洲欧洲日产国码一级毛片| 国产午夜免费高清久久影院| 亚洲日本在线免费观看| 免费无码不卡视频在线观看| 成人一级免费视频| 亚洲婷婷综合色高清在线| 麻豆国产入口在线观看免费| 一边摸一边桶一边脱免费视频| 亚洲电影免费在线观看| 成人免费在线观看网站| 十八禁的黄污污免费网站| 精品无码一区二区三区亚洲桃色| 青青青青青青久久久免费观看| 国产免费区在线观看十分钟| 中中文字幕亚洲无线码| 在线亚洲精品福利网址导航| 亚洲毛片在线免费观看| jizz免费一区二区三区|