日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# 连续抓取页面内容

發(fā)布時(shí)間:2025/4/16 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# 连续抓取页面内容 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

實(shí)現(xiàn)功能:去一個(gè)url抓取頁面,在頁面的內(nèi)容里面在去找另一個(gè)url。找到這個(gè)這url之后經(jīng)過一系列操作后再去重組的url去抓取內(nèi)容。

第一、寫出c#抓取頁面的代碼

c#抓取頁面 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.IO; 6 using System.Net; 7 using System.Text; 8 9 /// <summary> 10 ///abc 的摘要說明 11 /// </summary> 12 public static class abc 13 { 14 /// <summary> 15 /// webRequest 模擬http get請(qǐng)求 16 /// </summary> 17 /// <param name="strUrl">請(qǐng)求的url</param> 18 /// <param name="encoding">編碼</param> 19 /// <returns>返回字符串</returns> 20 public static string GetHttpResponse(this string strUrl, Encoding encoding) 21 { 22 string strResult = string.Empty; 23 try 24 { 25 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); 26 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); 27 Stream myStream = HttpWResp.GetResponseStream(); 28 StreamReader sr = new StreamReader(myStream, encoding); 29 strResult = sr.ReadToEnd(); 30 31 } 32 catch (Exception ex) 33 { 34 WriteLog(ex.Message, strUrl); 35 } 36 37 return strResult; 38 } 39 40 /// <summary> 41 /// webRequest 模擬http post請(qǐng)求 42 /// </summary> 43 /// <param name="url">請(qǐng)求的url</param> 44 /// <param name="val">post 的數(shù)據(jù)</param> 45 /// <returns>返回字符串</returns> 46 public static string GetHttpPostResponse(this string url, string val, Encoding encoding) 47 { 48 string strResult = string.Empty; 49 try 50 { 51 HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url); 52 myReq.Method = "Post"; 53 myReq.ContentType = "application/x-www-form-urlencoded"; 54 byte[] byteArray = encoding.GetBytes(val); 55 myReq.ContentLength = byteArray.Length; 56 Stream stream = myReq.GetRequestStream(); 57 stream.Write(byteArray, 0, byteArray.Length); 58 stream.Close(); 59 HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); 60 Stream myStream = HttpWResp.GetResponseStream(); 61 StreamReader sr = new StreamReader(myStream, encoding); 62 strResult = sr.ReadToEnd(); 63 64 } 65 catch (Exception ex) 66 { 67 WriteLog(ex.Message + val, url); 68 } 69 70 return strResult; 71 } 72 73 public static void WriteLog(string sLog, string titleLog) 74 { 75 try 76 { 77 string logPath = System.AppDomain.CurrentDomain.BaseDirectory;//目錄位置 78 79 DateTime dt = DateTime.Now; 80 string logfile = new StringBuilder(logPath).Append("\\Log\\").Append(dt.ToString("yyyy-MM-dd")).Append("\\").Append(titleLog).Append("_").Append(dt.ToString("yyyyMMddHHmmss")).Append(".txt").ToString(); 81 if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(logfile))) 82 { 83 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(logfile)); 84 } 85 if (!File.Exists(logfile)) 86 { 87 FileStream fs = System.IO.File.Create(logfile); 88 fs.Close(); 89 } 90 using (StreamWriter sw = new StreamWriter(logfile, true)) 91 { 92 sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":"); 93 sw.WriteLine(sLog); 94 sw.WriteLine(); 95 sw.Close(); 96 } 97 } 98 catch 99 { 100 101 } 102 } 103 }


第二、調(diào)用里面的方法GetHttpResponse去抓取頁面(注這是get方式,如果是post方式可以選擇post方式)

第三、用正則匹配方式得到想要的URL(Match mc = Regex.Match(aa, "action=(.*)>", RegexOptions.IgnoreCase);)

第四、由于此時(shí)得到URL是經(jīng)過瀏覽器處理的URL如果我們直接去抓取頁面就會(huì)找不到頁面。因?yàn)榇藭r(shí)的URL的協(xié)議是Https協(xié)議。所以我們需要中間做一次跳轉(zhuǎn)。

首先還是去抓aa = abc.GetHttpResponse(str, Encoding.UTF8);得到的URL里有一個(gè)將要跳轉(zhuǎn)到的url目錄。我們需要將主域名+剛剛得到的這個(gè)目錄。

第五、然后再去抓取。就可以得到我們想要的內(nèi)容!

轉(zhuǎn)載于:https://www.cnblogs.com/honghong75042/archive/2013/04/28/3049201.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的c# 连续抓取页面内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。