C# 如何使用HtmlAlityPack提取该字符串?

C# 如何使用HtmlAlityPack提取该字符串?,c#,html-agility-pack,C#,Html Agility Pack,我想从HTML源中提取内容值: <span itemprop="price" content="164,06"></span> 谢谢 你可以这样写: private string getTextfrom() { HtmlDocument doc = new HtmlDocument(); //doc.LoadHtml("<span itemprop=\"price\" content=\"164,06\"></span>");

我想从HTML源中提取
内容
值:

<span itemprop="price" content="164,06"></span>

谢谢

你可以这样写:

private string getTextfrom()
{
    HtmlDocument doc = new HtmlDocument();
    //doc.LoadHtml("<span itemprop=\"price\" content=\"164,06\"></span>");
    string htmlContent = GetPageContent("http://pastebin.com/raw.php?i=zE31NWtU");
    doc.LoadHtml(htmlContent);

    HtmlNode priceNode = doc.DocumentNode.SelectNodes("//span")[0];
    HtmlAttribute valueAttribute = priceNode.Attributes["content"];
    return valueAttribute.Value;
}

public static String GetPageContent(string Url)
{

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
    myRequest.Method = "GET";
    WebResponse myResponse = myRequest.GetResponse();
    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    string result = sr.ReadToEnd();
    sr.Close();
    myResponse.Close();

    return result;
}
私有字符串getTextfrom()
{
HtmlDocument doc=新的HtmlDocument();
//doc.LoadHtml(“”);
字符串htmlContent=GetPageContent(“http://pastebin.com/raw.php?i=zE31NWtU");
doc.LoadHtml(htmlContent);
HtmlNode priceNode=doc.DocumentNode.SelectNodes(“//span”)[0];
HtmlAttribute valueAttribute=priceNode.Attributes[“内容”];
返回valueAttribute.Value;
}
公共静态字符串GetPageContent(字符串Url)
{
HttpWebRequest myRequest=(HttpWebRequest)WebRequest.Create(Url);
myRequest.Method=“GET”;
WebResponse myResponse=myRequest.GetResponse();
StreamReader sr=新的StreamReader(myResponse.GetResponseStream(),System.Text.Encoding.UTF8);
字符串结果=sr.ReadToEnd();
高级关闭();
myResponse.Close();
返回结果;
}

如果HTML文件实际上包含3个或更多的span,该怎么办。如果看到以下行:HtmlNode priceNode=doc.DocumentNode.SelectNodes(“//span”)[0];在本文中,作者只保留了第一个span,在您keepHtmlNode priceNode=doc.DocumentNode.SelectNodes(//span)中,选择所有span标记,然后其余的工作正常。非常感谢!我还建议您使用SelectSingle note将其固定为一个音符:P
private string getTextfrom()
{
    HtmlDocument doc = new HtmlDocument();
    //doc.LoadHtml("<span itemprop=\"price\" content=\"164,06\"></span>");
    string htmlContent = GetPageContent("http://pastebin.com/raw.php?i=zE31NWtU");
    doc.LoadHtml(htmlContent);

    HtmlNode priceNode = doc.DocumentNode.SelectNodes("//span")[0];
    HtmlAttribute valueAttribute = priceNode.Attributes["content"];
    return valueAttribute.Value;
}

public static String GetPageContent(string Url)
{

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
    myRequest.Method = "GET";
    WebResponse myResponse = myRequest.GetResponse();
    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
    string result = sr.ReadToEnd();
    sr.Close();
    myResponse.Close();

    return result;
}