winfrom 从网页中通过源代码截取文章
生活随笔
收集整理的這篇文章主要介紹了
winfrom 从网页中通过源代码截取文章
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;//引用流
using System.Net;
using System.Text.RegularExpressions;//引用網(wǎng)頁namespace WindowsFormsApplication8
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private string thtxt(string q){Regex reg = new Regex("<(.|\n)+?>");//Regex r = new Regex(@"\s+");//把空格替換掉的正則表達式string th = reg.Replace(q, "");th = th.Replace("<", "<");th = th.Replace(">", "");//th = r.Replace(th,"");return th;}private void button1_Click(object sender, EventArgs e)//抓取網(wǎng)頁,在richtextbox1中顯示出網(wǎng)頁的源代碼
{WebRequest wr = WebRequest.Create(textBox1.Text.Trim());//接收按鈕2里輸入的網(wǎng)址的文本內(nèi)容WebResponse wtr = wr.GetResponse();//得到一個網(wǎng)址的回應Stream a = wtr.GetResponseStream();//用流來讀取StreamReader s = new StreamReader(a,Encoding.UTF8);string q= s.ReadToEnd();//定義一個string類型的來接受它string th = thtxt(q);//調(diào)用上面去亂碼的函數(shù),把里面的符號全部去掉。a.Close();//使用完把流給關掉richTextBox1.Text = th;StreamWriter c = new StreamWriter("E:\\1.txt");//桌面上新建一個測試的內(nèi)容,吧截下來的文本保存在這個文本檔里。//把截取好的文本內(nèi)容從流里輸出出來c.WriteLine(th);//輸出c.Close();//把流給關掉
wtr.Close();if (q.IndexOf("紅|袖|言|情|小|說") > 0){int sub = q.IndexOf("紅|袖|言|情|小|說");//添加一個網(wǎng)址的索引int xub = q.IndexOf("最后放棄");//從這里截取//+4 (“好了,收工”)這4個字的索引不被截取,+4就是這五個字都顯示在文本,否則會被刪掉string w = q.Substring(sub, xub - sub + 4);richTextBox1.Text = w;//讓顯示框里吧截取好的文本內(nèi)容給顯示出來。//引用一個流把它截取下來的內(nèi)容存到一個文件里
}else{MessageBox.Show("您沒有獲得數(shù)據(jù)");}}}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/w-wz/p/4587711.html
總結
以上是生活随笔為你收集整理的winfrom 从网页中通过源代码截取文章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WC总结
- 下一篇: 2、在控制台上输出1—100数字