问题
i'm trying to get some attributes for HTML tags in web page for example
<html>
<head>
<title>test page</title>
</head>
<body>
<div id="header" class="clearit" role="banner">
<div id="headerWrapper">
<ul id="primaryNav" role="navigation">
<li id="musicNav" class="navItem">
<a href="/music" class="nav-link">Music</a>
</li>
<li id="listenNav" class="navItem">
<a href="/listen" class="nav-link">Radio</a>
</li>
<li id="eventsNav" class="navItem">
<a href="/events" class="nav-link">Events</a>
</li>
<li id="chartsNav" class="navItem">
<a href="/charts" class="nav-link">Charts</a>
</li>
<li id="communityNav" class="navItem">
<a href="/community" class="nav-link">Community</a>
</li>
<li id="originalsNav" class="navItem">
<a href="http://originals.last.fm" class="nav-link">Originals</a>
</li>
</ul>
</div>
</div>
</body>
</html>
for example i need the actual Height and width for #headerWrapper and compare it with #musicNav in my PHP program , since php is server side i can't get these attribute so i'm thinking to append javascript code to calculate these attribute and store it in json file like this code
<script type="text/javascript">
document.ready(function() {
var JSONObject= {
"tagname":"headerWrapper",
"height":$("#headerWrapper").height(),
"width":$("#headerWrapper").width()
},
{
"tagname":"musicNav",
"height":$("#musicNav").height(),
"width":$("#musicNav").width()
}
});
});
</script>
then read it by php file contain my algorithm witch extract visual features from webpages.
but my problem is i need to render the webpage with appended javascript using some browser or rendering engine in PHP or java ... so dose any one have some thing like that? is my method right or there is better solution?
回答1:
Incase you want to render a webpage given a url and need an api to walk through the rendered dom
Phantomjs and its api and examples will help you. Check out open
render
PhantomJS is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
回答2:
If i understand you correctly, you need a means to control the browser from your java app. This seems to be relevant. Things you may also want to account for --
- Letting the app know about your browser(binary or otherwise).
- Choosing from multiple available browsers on the host.
- Account for cross-platform support.
来源:https://stackoverflow.com/questions/11246700/render-html-pages-in-php-or-java