C# 使用 html Agility Pack 选择属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/541953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting attribute values with html Agility Pack
提问by Vegar
I'm trying to retrieve a specific image from a html document, using html agility pack and this xpath:
我正在尝试使用 html agility pack 和此 xpath 从 html 文档中检索特定图像:
//div[@id='topslot']/a/img/@src
As far as I can see, it finds the src-attribute, but it returns the img-tag. Why is that?
据我所知,它找到了 src-attribute,但它返回了 img-tag。这是为什么?
I would expect the InnerHtml/InnerText or something to be set, but both are empty strings. OuterHtml is set to the complete img-tag.
我希望 InnerHtml/InnerText 或其他东西被设置,但两者都是空字符串。OuterHtml 设置为完整的 img-tag。
Are there any documentation for Html Agility Pack?
是否有 Html Agility Pack 的任何文档?
采纳答案by Azat Razetdinov
Html Agility Pack does not supportattribute selection.
Html Agility Pack不支持属性选择。
回答by Almas
Html Agility Pack will support it soon.
Html Agility Pack 将很快支持它。
http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=204342
http://htmlagilitypack.codeplex.com/Thread/View.aspx?ThreadId=204342
回答by Pierluc SS
You can directly grab the attribute if you use the HtmlNavigator
instead.
如果使用HtmlNavigator
代替,则可以直接获取该属性。
//Load document from some html string
HtmlDocument hdoc = new HtmlDocument();
hdoc.LoadHtml(htmlContent);
//Load navigator for current document
HtmlNodeNavigator navigator = (HtmlNodeNavigator)hdoc.CreateNavigator();
//Get value from given xpath
string xpath = "//div[@id='topslot']/a/img/@src";
string val = navigator.SelectSingleNode(xpath).Value;
回答by Ben
You may use the method "GetAttributeValue".
您可以使用“GetAttributeValue”方法。
Example:
例子:
//[...] code before needs to load a html document
HtmlAgilityPack.HtmlDocument htmldoc = e.Document;
//get all nodes "a" matching the XPath expression
HtmlNodeCollection AllNodes = htmldoc.DocumentNode.SelectNodes("*[@class='item']/p/a");
//show a messagebox for each node found that shows the content of attribute "href"
foreach (var MensaNode in AllNodes)
{
string url = MensaNode.GetAttributeValue("href", "not found");
MessageBox.Show(url);
}
回答by Brett Jones
Reading and Writing Attributes with Html Agility Pack
使用 Html Agility Pack 读写属性
You can both read and set the attributes in HtmlAgilityPack. This example selects the < html> tag and selects the 'lang' (language) attribute if it exists and then reads and writes to the 'lang' attribute.
您可以在 HtmlAgilityPack 中读取和设置属性。此示例选择 <html> 标记并选择 'lang'(语言)属性(如果存在),然后读取和写入 'lang' 属性。
In the example below, the doc.LoadHtml(this.All), "this.All" is a string representation of a html document.
在下面的例子中,doc.LoadHtml(this.All),“this.All”是一个 html 文档的字符串表示。
Read and write:
读和写:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(this.All);
string language = string.Empty;
var nodes = doc.DocumentNode.SelectNodes("//html");
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i] != null && nodes[i].Attributes.Count > 0 && nodes[i].Attributes.Contains("lang"))
{
language = nodes[i].Attributes["lang"].Value; //Get attribute
nodes[i].Attributes["lang"].Value = "en-US"; //Set attribute
}
}
Read only:
只读:
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(this.All);
string language = string.Empty;
var nodes = doc.DocumentNode.SelectNodes("//html");
foreach (HtmlNode a in nodes)
{
if (a != null && a.Attributes.Count > 0 && a.Attributes.Contains("lang"))
{
language = a.Attributes["lang"].Value;
}
}
回答by Abhay Shiro
I used the following way to obtain the attributes of an image.
我使用以下方式获取图像的属性。
var MainImageString = MainImageNode.Attributes.Where(i=> i.Name=="src").FirstOrDefault();
You can specify the attribute name to get its value; if you don't know the attribute name, give a breakpoint after you have fetched the node and see its attributes by hovering over it.
您可以指定属性名称以获取其值;如果您不知道属性名称,请在获取节点后给出断点并通过将鼠标悬停在其上来查看其属性。
Hope I helped.
希望我有所帮助。