以下代码仅返回“干得好!” 如何从中获取实际的 URL?我按照给出的网站上的教程进行操作,但我仍然很难理解它。另外,我认为这不是处理正则表达式(将正则表达式与 html 混合)的最佳方法。有没有一种简单的方法来基于它的 CSS 类来捕获文本?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace Scraper
{
class Program
{
static void Main(string[] args)
{
string target = @"http://www.omegacoder.com/?p=58";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(target);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Regex URL = new Regex("(?:href=)(?<link>.*?)");
string line;
using (Stream responseStream = response.GetResponseStream())
using (StreamReader htmlStream = new StreamReader(responseStream))
while ((line = htmlStream.ReadLine()) != null){
Match m = URL.Match(line);
if (m.Success) {
Console.WriteLine("Good job! " + URL.Match(line) + m.Groups[0].Value + m.Groups[1].Value + m.Groups["link"]);
Console.ReadLine();
} else {
}
}
/* if (Regex.IsMatch(line, "XXXXX"))
Console.WriteLine(line);
} */
Console.ReadLine();
}
}
}