C# 使用C从网页获取链接#
我试图在网页上搜寻文章的链接 这是我的代码:C# 使用C从网页获取链接#,c#,visual-studio,C#,Visual Studio,我试图在网页上搜寻文章的链接 这是我的代码: static void Main(string[] args) { WebClient web = new WebClient(); string html = web.DownloadString("http://www.dailymirror.lk"); MatchCollection m1 = Regex.Matches(html, @"<a href=""(.+?)""/s*class=""panel-headi
static void Main(string[] args)
{
WebClient web = new WebClient();
string html = web.DownloadString("http://www.dailymirror.lk");
MatchCollection m1 = Regex.Matches(html, @"<a href=""(.+?)""/s*class=""panel-heading"">",RegexOptions.Singleline);
foreach(Match m in m1)
{
Console.WriteLine(m.Groups[1].Value);
}
}
static void Main(字符串[]args)
{
WebClient web=新的WebClient();
字符串html=web.DownloadString(“http://www.dailymirror.lk");
MatchCollection m1=Regex.Matches(html,@“”,RegexOptions.Singleline);
foreach(匹配m1中的m)
{
Console.WriteLine(m.Groups[1].Value);
}
}
我在页面中重点关注的html标记如下:
<a href="http://www.dailymirror.lk/99833/ravi-s-budget-blues" class="panel-heading">
但是,我的代码无法检索链接,我是否可以修改我的代码?如上所述,用正则表达式解析html通常是个坏主意 一种方法是使用HTML敏捷包:
用正则表达式刮去HTML通常是个坏主意。使用适当的库,例如:)@IanP——有人将粘贴该链接。我认为这是强制性的
HtmlWeb hw = new HtmlWeb();
HtmlDocument doc = hw.Load("http://www.mywebsite.com");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href]"))
{
// do something with link here
}