获取一个HTML元素的值 [英] Get the value of an HTML element

查看:380
本文介绍了获取一个HTML元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,网页的HTML代码。我想我的程序返回是一个标记中的值。例如。我想获得朱利叶斯出来

I have the HTML code of a webpage in a text file. I'd like my program to return the value that is in a tag. E.g. I want to get "Julius" out of

<span class="hidden first">Julius</span>



我需要为这个正则表达式?否则,什么是一个字符串函数,能做到吗?

Do I need regular expression for this? Otherwise what is a string function that can do it?

推荐答案

您应该使用一个HTML解析器像的 htmlagilitypack .Regex不是解析HTML文件作为HTML不严格,也不是经常用它的格式是不错的选择。

You should be using an html parser like htmlagilitypack .Regex is not a good choice for parsing HTML files as HTML is not strict nor is it regular with its format.

您可以使用下面的代码中使用 HtmlAgilityPack

You can use below code to retrieve it using HtmlAgilityPack

HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);

var itemList = doc.DocumentNode.SelectNodes("//span[@class='hidden first']")//this xpath selects all span tag having its class as hidden first
                  .Select(p => p.InnerText)
                  .ToList();

//itemList now contain all the span tags content having its class as hidden first

这篇关于获取一个HTML元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆