如何解析HTML并获得CSS样式 [英] How to parse HTML and get CSS styles
问题描述
我需要解析HTML并找到相应的CSS样式。我可以分别解析HTML和CSS,但我无法将它们组合起来。例如,我有一个像这样的XHTML页面:
I need to parse HTML and find corresponding CSS styles. I can parse HTML and CSS separataly, but I can't combine them. For example, I have an XHTML page like this:
<html>
<head>
<title></title>
</head>
<body>
<div class="abc">Hello World</div>
</body>
</html>
我必须搜索hello world并找到它的类名,之后我需要从外部CSS文件中找到它的样式。使用Java,JavaScript和PHP的答案都可以。
I have to search for "hello world" and find its class name, and after that I need to find its style from an external CSS file. Answers using Java, JavaScript, and PHP are all okay.
推荐答案
使用 jsoup 库,它是一个HTML Parser。你可以看看例如这里
例如你可以做类似的事情这个:
Use jsoup library in java which is a HTML Parser. You can see for example here
For example you can do something like this:
String html="<<your html content>>";
Document doc = Jsoup.parse(html);
Element ele=doc.getElementsContainingOwnText("Hello World").first.clone(); //get tag containing Hello world
HashSet<String>class=ele.classNames(); //gives you the classnames of element containing Hello world
您可以进一步探索库以满足您的需求。
You can explore the library further to fit your needs.
这篇关于如何解析HTML并获得CSS样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!