xpath php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
$target_url = "http://www.crazyant.net/1973.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
//创建一个DomDocument对象,用于处理一个HTML
$dom = new DOMDocument();
//从一个字符串加载HTML
@$dom->loadHTML($html);
//使该HTML规范化
$dom->normalize();
//*[@id="post-1980"]/div/header/h2/a
//用DOMXpath加载DOM,用于查询
$xpath = new DOMXPath($dom);
#获取所有的a标签的地址
$hrefs = $xpath->evaluate("/html/body//a//@href");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$linktext = $href->nodeValue;
echo $linktext;
echo "<br />";
}
?>
你可能想看:
PHP7.0~PHP7.1~PHP7.2~PHP7.3~PHP7.4新特性php 7.4
PHP7.0~PHP7.1~PHP7.2~PHP7.3~PHP7.4新特性php 7.2 7.3
PHP之——在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。wampserver打开php项目
PHP之——在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。wampserver开发php网页
PHP配置指令作用域说明(PHP,INI,PERDIR、PHP,INI,SYSTEM、PHP,INI,USER、PHP,INI,ALL)php 作用域
PHP - 什么是 PHP? 为什么用 PHP? 有谁在用 PHP?为什么说php
[PHP] PHP的纯CPU基准测试(PHP5.5.9 vs PHP7.2.1)php性能测试
[PHP问题]PHP Warning: PHP Startup: Unable to load dynamic library ‘C:/AppServ\php5php基础问题
PHP加密方法-用Zend Encoder加密PHP文件和PHP 优化配置(PHP文件加密)php zend解密
PHP常量PHP,SAPI与函数php,sapi,name()简介,PHP运行环境检测php sapi
11.PHP内核探索:嵌入式PHP PHP内核探索:嵌入式PHP嵌入式PHP