用 PHP 或 Java 呈现 HTML 页面 [英] Render HTML pages in PHP or Java

查看:55
本文介绍了用 PHP 或 Java 呈现 HTML 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取网页中 HTML 标签的一些属性,例如

<头><title>测试页</title><身体><div id="header" class="clearit" role="banner"><div id="headerWrapper"><ul id="primaryNav" role="导航"><li id="musicNav" class="navItem"><a href="/music" class="nav-link">音乐</a><li id="listenNav" class="navItem"><a href="/listen" class="nav-link">收音机</a><li id="eventsNav" class="navItem"><a href="/events" class="nav-link">事件</a><li id="chartsNav" class="navItem"><a href="/charts" class="nav-link">图表</a><li id="communityNav" class="navItem"><a href="/community" class="nav-link">社区</a><li id="originalsNav" class="navItem"><a href="http://originals.last.fm" class="nav-link">Originals</a>

</html>

例如,我需要#headerWrapper 的实际高度和宽度,并将其与我的 PHP 程序中的 #musicNav 进行比较,因为 php 是服务器端我无法获得这些属性,所以我想附加 javascript 代码来计算这些属性并将其存储在 json 文件中,如此代码

然后通过包含我的算法的 php 文件读取它,女巫从网页中提取视觉特征.

但我的问题是我需要使用某些浏览器或 PHP 或 Java 中的渲染引擎来渲染带有附加 javascript 的网页……所以有人有类似的东西吗?我的方法是对的还是有更好的解决方案?

解决方案

如果你想渲染一个给定 url 的网页并且需要一个 api 来遍历渲染的 domPhantomjs 及其 apiexamples 会对您有所帮助.查看open render

<块引用>

PhantomJS 是一个带有 JavaScript API 的无头 WebKit.它具有快速和对各种 Web 标准的原生支持:DOM 处理、CSS 选择器、JSON、画布和 SVG.

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?

解决方案

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.

这篇关于用 PHP 或 Java 呈现 HTML 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆