導航:首頁 > 編程語言 > 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