如何从C#中的字符串中提取href标记?
我有一个方法返回以下格式的字符串:如何从C#中的字符串中提取href标记?,c#,string,tags,href,C#,String,Tags,Href,我有一个方法返回以下格式的字符串: string tableTag = "<th><a href="Boot_53.html">135 Boot</a></th>" 换句话说,link应该在字符串中指定href属性。如何才能做到这一点?用于解析HTML: var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml( tableTag ); string link = doc.Docume
string tableTag = "<th><a href="Boot_53.html">135 Boot</a></th>"
换句话说,link
应该在字符串中指定href
属性。如何才能做到这一点?用于解析HTML:
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml( tableTag );
string link = doc.DocumentNode.SelectSingleNode("//a").Attributes["href"].Value;
您可以使用HTML解析器,如解析输入HTML并提取您要查找的信息:
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var doc = new HtmlDocument();
string tableTag = "<th><a href=\"Boot_53.html\">135 Boot</a></th>";
doc.LoadHtml(tableTag);
var anchor = doc.DocumentNode.SelectSingleNode("//a");
if (anchor != null)
{
string link = anchor.Attributes["href"].Value;
Console.WriteLine(link);
}
}
}
使用HtmlAgilityPack;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
班级计划
{
静态void Main(字符串[]参数)
{
var doc=新的HtmlDocument();
字符串tableTag=“”;
doc.LoadHtml(tableTag);
var anchor=doc.DocumentNode.SelectSingleNode(“//a”);
if(锚点!=null)
{
字符串链接=anchor.Attributes[“href”].Value;
控制台写入线(链接);
}
}
}
您可以使用正则表达式:
string input= "<th><a href=\"Boot_53.html\">135 Boot</a></th>";
string regex= "href=\"(.*)\"";
Match match = Regex.Match(input, regex);
if (match.Success)
{
string link= match.Groups[1].Value;
Console.WriteLine(link);
}
字符串输入=”;
字符串regex=“href=\”(.*)\”;
Match Match=Regex.Match(输入,Regex);
如果(匹配成功)
{
字符串链接=匹配。组[1]。值;
控制台写入线(链接);
}
如果您知道html实际上是一个xhtml(一种或多或少符合xml标准的html),那么您只需使用专用于xml的工具(通常比用于html的工具简单)即可解析
var hrefLink=XElement.Parse(“”)
.后代(“a”)
.Select(x=>x.Attribute(“href”).Value)
.FirstOrDefault();
您可以将其用作HtmlAlityPack的替代方案:
var context = BrowsingContext.New(Configuration.Default);
string tableTag = "<th><a href=\"Boot_53.html\">135 Boot</a></th>";
var document = await context.OpenAsync(req => req.Content(tableTag));
var anchor = document.All.FirstOrDefault(x => x.LocalName == "a");
if (anchor != null)
{
string link = anchor.GetAttribute("href"); // "Boot_53.html"
}
var context=BrowsingContext.New(Configuration.Default);
字符串tableTag=“”;
var document=wait context.OpenAsync(req=>req.Content(tableTag));
var anchor=document.All.FirstOrDefault(x=>x.LocalName==“a”);
if(锚点!=null)
{
string link=anchor.GetAttribute(“href”);/“Boot_53.html”
}
如何在C#中做到这一点?
不是一个好问题。展示你到目前为止所做的尝试,以及你遇到的困难。看看这个答案
var hrefLink = XElement.Parse("<th><a href=\"Boot_53.html\">135 Boot</a></th>")
.Descendants("a")
.Select(x => x.Attribute("href").Value)
.FirstOrDefault();
var context = BrowsingContext.New(Configuration.Default);
string tableTag = "<th><a href=\"Boot_53.html\">135 Boot</a></th>";
var document = await context.OpenAsync(req => req.Content(tableTag));
var anchor = document.All.FirstOrDefault(x => x.LocalName == "a");
if (anchor != null)
{
string link = anchor.GetAttribute("href"); // "Boot_53.html"
}