use regular expressions to filter out tags, for example (might not always work, for example, with <script>...):str = System.Text.RegularExpressions.Regex.Replace(YourString,@"<[^>]+>","");
do multiple passes, for examplestr = System.Text.RegularExpressions.Regex.Replace(YourString,@"<script[^>]*>.*?</script>","",RegexOptions.IgnoreCase);str = System.Text.RegularExpressions.Regex.Replace(YourString,@"<style[^>]*>.*?</style>","",RegexOptions.IgnoreCase); str = System.Text.RegularExpressions.Regex.Replace(YourString,@"<[^>]+>","");
引用别人的提取文本的程序,你试试: s is the source code of the webpage.private String fetchText(String s) { //Filter out HTML and JavaScript from the page, leaving only body text s = Convert.ToString(Regex.Match(s, @"<body.+?</body>", RegexOptions.Singleline | RegexOptions.IgnoreCase)); //strip everything but <BODY> s = Regex.Replace(s, "<script[^>]*?>.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase); //strip JavaScript s = Regex.Replace(s, "<[^>]*>", ""); //strip HTML tags s = Regex.Replace(s, "&(copy|#169);|&(quot|#34);|&(amp|#38);|&(lt|#60);&(gt|#62);|&(nbsp|#160);|&(iexcl|#161);|&(cent|#162);|&(pound|#163);|·", " "); //strip symbols s = s.Replace("\t", " "); //strip tabs s = Regex.Replace(s, "([\r\n])+", " "); //strip carriage returns s = Regex.Replace(s, "\\s\\s+", " "); //strip white space (must be last) return s.Trim(); }
But your way is useful, thanks a lot!Any more hints?
<a href="xxxxxxxxxxxx">dddddddddd</a>
str = System.Text.RegularExpressions.Regex.Replace(YourString,@"<[^>]+>","");
s is the source code of the webpage.private String fetchText(String s)
{
//Filter out HTML and JavaScript from the page, leaving only body text
s = Convert.ToString(Regex.Match(s, @"<body.+?</body>", RegexOptions.Singleline | RegexOptions.IgnoreCase)); //strip everything but <BODY>
s = Regex.Replace(s, "<script[^>]*?>.*?</script>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase); //strip JavaScript
s = Regex.Replace(s, "<[^>]*>", ""); //strip HTML tags
s = Regex.Replace(s, "&(copy|#169);|&(quot|#34);|&(amp|#38);|&(lt|#60);&(gt|#62);|&(nbsp|#160);|&(iexcl|#161);|&(cent|#162);|&(pound|#163);|·", " "); //strip symbols
s = s.Replace("\t", " "); //strip tabs
s = Regex.Replace(s, "([\r\n])+", " "); //strip carriage returns
s = Regex.Replace(s, "\\s\\s+", " "); //strip white space (must be last)
return s.Trim();
}
不过注释是日文的,我尽量给你翻译.
-------------*********源代码*******---------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Web;
using System.Net;namespace HTMLGet
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//译文:对 windows form designer supprot. 必要的.
InitializeComponent(); //
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//译文:调用InitializeComponent 后,请添加construct code.
} /// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///译文:这是对 windows form designer supprot的必要的 修改,补丁(mend).这一小块的代码或数据请勿变动.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// アプリケーションのメイン エントリ ポイントです。
///译文:这里是application’s main 或enter point .
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
Read();
} private void Read()
{
Stream stream = null;
StreamReader sr = null; try
{
WebRequest webReq = HttpWebRequest.Create( "http://xww.fxsz.com.cn" );
webReq.Method = "GET";
// 1秒でタイムアウトさせる。译文:timeout 设为1秒.
webReq.Timeout = 1000000;
// IE のプロキシ設定を使用する。译文:使用IE的proxy设定
webReq.Proxy = System.Net.WebProxy.GetDefaultProxy(); WebResponse webRes = webReq.GetResponse();
// HttpWebRequest からストリームを取得する。
//译文:从HttpWebRequest得到 stream
stream = webRes.GetResponseStream();
// 1行ごとに扱いたいので、StreamReader にする。
//译文:在streamreader 中想得到一行
sr=new System.IO.StreamReader(stream, Encoding.GetEncoding("x-euc-jp")); string str;
str = sr.ReadToEnd();
MessageBox.Show(str);
//Debug.WriteLine(str);
}
catch (Exception exc)
{
// わかりやすいメッセージに変える。
//变成可以理解的message
//throw(new Exception("xxxに接続できませんでした。"));
throw(new Exception("不能连接到XXX"));
}
finally
{
if (sr != null) sr.Close();
if (stream != null) stream.Close();
}
}
}
}