导航:首页 > 编程语言 > phpdom操作

phpdom操作

发布时间:2023-01-20 23:26:57

1. 使用php如何获取微信文章

可以尝试使用DOM操作库simple-html-dom.php,快速获取HTML结构的内容:
<?php
require dirname(__FILE__).'/simple_html_dom.php';
$html = file_get_html('http://php.net/');
$articles = array();
foreach($html->find('article.newsentry') as $article) {
$item['time'] = trim($article->find('time', 0)->plaintext);
$item['title'] = trim($article->find('h2.newstitle', 0)->plaintext);
$item['content'] = trim($article->find('div.newscontent', 0)->plaintext);
$articles[] = $item;
}
print_r($articles);
可以把抓取到的内容写入置于内存上的SQLite(/run/shm/php/crawler.db3),避免频繁的磁盘IO.

2. php中调用html中的元素

要想利用php调用html中元素,需要借助PHP Simple HTML DOM Parser这个工具。
它可以以类似jQuery的方式通过css选择器来返回指定的DOM元素,功能十分强大。
1、首先要在程序的开始引入simple_html_dom.php这个文件 :
include_once('simple_html_dom.php');
2、PHP Simple HTML DOM Parser提供了3种方式来创建DOM对象
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
3、得到DOM对象后就可以进行各种操作了
// Find all anchors, returns a array of element objects
$ret = $html->find('a');
// Find (N)th anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', 0);
// Find lastest anchor, returns element object or null if not found (zero based)
$ret = $html->find('a', -1);
// Find all <div> with the id attribute
$ret = $html->find('div[id]');
// Find all <div> which attribute id=foo
$ret = $html->find('div[id=foo]');

阅读全文

与phpdom操作相关的资料

热点内容
php获取原始数据 浏览:244
pic单片机如何编程 浏览:888
javabyte写文件 浏览:319
java获取类包名 浏览:888
ftp命令编码格式 浏览:298
程序员那么可爱陆离穿正装 浏览:169
源码的账号密码在哪个文件 浏览:437
如何在中国农业银行app绑定银行卡 浏览:990
shopnum1多用户商城系统源码 浏览:744
红包广告平台源码 浏览:772
硬盘格式化时用的dos命令是 浏览:942
找人缓解压力 浏览:937
iphone的pdf 浏览:351
90压缩饼干怎么吃 浏览:668
php教材下载 浏览:915
什么解压密码最好 浏览:590
数据库与服务器如何连接 浏览:444
架构师需要阅读的源码 浏览:483
ch编译器 浏览:456
java必须自己写一个编译器吗 浏览:944