PHP简单DOM解析器解析当前PHP页面

时间:2012-12-06 00:41:33

标签: php dom html-parsing

我正在使用PHP Simple DOM解析器来提取给定页面上的所有图像源,如下所示:

// Include the library
include('simple_html_dom.php');

// Retrieve the DOM from a given URL
$html = file_get_html('http://google.com/');

// Retrieve all images and print their SRCs
foreach($html->find('img') as $e)
    echo $e->src . '<br>';

我希望在Wordpress的管理(后端)区域使用一个页面,而不是使用Google.com。这些页面是PHP页面,而不是HTML(但页面始终具有标准HTML)。如何将当前页面用作$html变量? PHP新手在这里。

1 个答案:

答案 0 :(得分:0)

使用此库dxtool找到here

登录

require 'WebGet.php';
$w = new WebGet();
// using cache to prevent repetitive download
$w->useCache = true;
$w->cacheLocation = '/tmp';
$w->cacheMaxAge = 3600;
$w->cookieFile = '/tmp/cookie.txt';

// $login_get_data and $login_post_data is associative array
$login = $w->requestContent($login_url, $login_get_data, $login_post_data);

访问包含页面

的图像
// $image_page_url is the url of the page where your images exist.
$image_page = $w->requestContent($image_page_url);

解析图像并显示

$dom = new DOMDocument();
$dom->loadHTML($image_page);
$imgs = $dom->getElementsByTagName("img");
foreach($imgs as $img){
    echo $img->getAttribute("src");
}

免责声明:我是本课程的作者