Re: [問題] 抓取網頁裡面的資訊

看板RegExp (正規表示式 Regular Expression)作者 (銘)時間15年前 (2009/08/21 20:44), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《gemini392 (銘)》之銘言: : 最近剛接觸正規表示法 : 想請問大家 : <td class="defaultstyle" align="left">想擷取下來的資訊</td> : 該如何寫會比較好?? : 目前我比較直覺想到的是 : '/<td class="defaultstyle" align="left">(.+?)<\/td>/' : 但改了又改就是抓不到 /_\ : 懇請大家幫忙解惑一下 : 謝謝各位 :) 以下附上程式碼,我是用C#寫的 麻煩大家幫忙解惑了 @@" using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections.Specialized; using System.Net; using System.Text.RegularExpressions; using System.IO; namespace Capture { public partial class Form1 : Form { private const string _borrowlist = "http://140.115.156.76/borrowlist.htm"; private const string _userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { WebClient webClient = new WebClient(); webClient.Headers[HttpRequestHeader.UserAgent] = _userAgent; webClient.Encoding = Encoding.UTF8; byte[] firstResponse = webClient.DownloadData(_borrowlist); string firstRes = Encoding.UTF8.GetString(firstResponse); Regex regex = new Regex("class=\"defaultstyle\" align=\"left\">(.+?)</td>", RegexOptions.IgnoreCase); MatchCollection matches = regex.Matches(firstRes); foreach (Match match in matches) { textBox1.Text = match.Value; } } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.156.76
文章代碼(AID): #1AZfQqwT (RegExp)
文章代碼(AID): #1AZfQqwT (RegExp)