ASP.net(C#)從其他網(wǎng)站抓取內(nèi)容并截取有用信息的實(shí)現(xiàn)代碼
更新時(shí)間:2011年09月24日 20:06:17 作者:
ASP.net(C#)從其他網(wǎng)站抓取內(nèi)容并截取有用信息的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
1. 需要引用的類庫
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
2. 獲取其他網(wǎng)站網(wǎng)頁內(nèi)容的關(guān)鍵代碼
WebRequest request = WebRequest.Create("http://目標(biāo)網(wǎng)址.com/");
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//reader.ReadToEnd() 表示取得網(wǎng)頁的源碼
TextBox1.Text = reader.ReadToEnd();
3. 獲取其他網(wǎng)站網(wǎng)頁源碼之后通過{正則表達(dá)式}帥選有用信息
MatchCollection TitleMatchs = Regex.Matches(reader.ReadToEnd(), @"發(fā)表評論</a></p></div><div class=""body"">([\s\S]*?)</div><div class=""share"">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match NextMatch in TitleMatchs)
{
s += "<br>" + NextMatch.Groups[1].Value;
TextBox1.Text += "\n" + NextMatch.Groups[1].Value;
}
RegexOptions.IgnoreCase: 表示不區(qū)分大小寫, 一般網(wǎng)站源碼大小寫不敏感所以取消之.
RegexOptions.Multiline: 表示對多行內(nèi)容進(jìn)行帥選.
4. 大功告成
不上圖了! 影響不好! 見諒見諒
文中代碼打包下載
復(fù)制代碼 代碼如下:
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
2. 獲取其他網(wǎng)站網(wǎng)頁內(nèi)容的關(guān)鍵代碼
復(fù)制代碼 代碼如下:
WebRequest request = WebRequest.Create("http://目標(biāo)網(wǎng)址.com/");
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//reader.ReadToEnd() 表示取得網(wǎng)頁的源碼
TextBox1.Text = reader.ReadToEnd();
3. 獲取其他網(wǎng)站網(wǎng)頁源碼之后通過{正則表達(dá)式}帥選有用信息
復(fù)制代碼 代碼如下:
MatchCollection TitleMatchs = Regex.Matches(reader.ReadToEnd(), @"發(fā)表評論</a></p></div><div class=""body"">([\s\S]*?)</div><div class=""share"">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match NextMatch in TitleMatchs)
{
s += "<br>" + NextMatch.Groups[1].Value;
TextBox1.Text += "\n" + NextMatch.Groups[1].Value;
}
RegexOptions.IgnoreCase: 表示不區(qū)分大小寫, 一般網(wǎng)站源碼大小寫不敏感所以取消之.
RegexOptions.Multiline: 表示對多行內(nèi)容進(jìn)行帥選.
4. 大功告成
不上圖了! 影響不好! 見諒見諒
文中代碼打包下載
您可能感興趣的文章:
- C#實(shí)現(xiàn)通過程序自動(dòng)抓取遠(yuǎn)程Web網(wǎng)頁信息的代碼
- asp.net c# 抓取頁面信息方法介紹
- c# HttpWebRequest通過代理服務(wù)器抓取網(wǎng)頁內(nèi)容應(yīng)用介紹
- C# 抓取網(wǎng)頁內(nèi)容的方法
- c#根據(jù)網(wǎng)址抓取網(wǎng)頁截屏生成圖片的示例
- c#實(shí)現(xiàn)抓取高清美女妹紙圖片
- C#抓取當(dāng)前屏幕并保存為圖片的方法
- C#實(shí)現(xiàn)抓取和分析網(wǎng)頁類實(shí)例
- C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實(shí)例
- 基于C#實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲 C#抓取網(wǎng)頁Html源碼
- C#抓取網(wǎng)頁數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
相關(guān)文章
asp.net Repeater 數(shù)據(jù)綁定代碼
asp.net Repeater 數(shù)據(jù)綁定代碼2010-03-03Asp.net core利用dynamic簡化數(shù)據(jù)庫訪問
這篇文章介紹了Asp.net core利用dynamic簡化數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07ASP .NET調(diào)用javascript中Response.Write和ClientScript.RegisterSta
最近在用ASP .NET的code behind 調(diào)用javascript中發(fā)現(xiàn)Response.Write不能拿到form的值,而ClientScript.RegisterStartupScript可以。2010-12-12.net?6項(xiàng)目實(shí)現(xiàn)壓縮發(fā)布
這篇文章介紹了.net?6項(xiàng)目實(shí)現(xiàn)壓縮發(fā)布的方式,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12