Php simplehtmldom-跟随链接
有人能举例说明如何跟踪每个元素的链接吗Php simplehtmldom-跟随链接,php,simple-html-dom,Php,Simple Html Dom,有人能举例说明如何跟踪每个元素的链接吗 纽约 美国 IT服务 然后: <div class="InfoD"> <h2>New York</h2> <h3>USA</h3> <strong>ITService</strong> <p> Tel. : XXXXXX </p> <p> Mail. : XXXX@XXX.com </p> </div&
纽约
美国
IT服务
然后:
<div class="InfoD">
<h2>New York</h2>
<h3>USA</h3>
<strong>ITService</strong>
<p>
Tel. : XXXXXX
</p>
<p>
Mail. : XXXX@XXX.com
</p>
</div>
纽约
美国
IT服务
电话:XXXXXX
邮件:XXXX@XXX.com
我知道如何使用HTMLDOM刮取这些元素,但是当每个元素和多个页面都有链接时,我不知道如何刮取。如果有人能指出一个例子或任何类似的教程。谢谢首先从
li获取所有链接。#选择一个
,然后循环从每个链接获取div.InfoD
下面是一段代码片段,展示了如何:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$url = "http://www.blabla.com/";
$baseUrl= "http://www.blabla.com"
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a URL
$html->load_file($url);
// Get all links
$anchors = $html->find('li.#Selected a');
// loop through each link and get the node having "InfoD" class
// Everytime make sure to clear dom objects to avoid memory leaks
foreach ($anchors as $anchor) {
// Create the new link to parse
$urlTemp = $baseUrl . $anchor->href;
//Create a DOM object
$html2 = new simple_html_dom();
// Load HTML from a URL
$html2->load_file($urlTemp);
// Get all nodes with "text-logo"
$div = $html->find('div.InfoD', 0);
echo $div;
echo "<hr/>";
// Clear dom object
$html2->clear();
unset($htm2);
}
// Clear dom object
$html->clear();
unset($html);
//包括简单的HTML DOM解析器
包括“simple_html_dom.php”;
$url=”http://www.blabla.com/";
$baseUrl=”http://www.blabla.com"
//创建DOM对象
$html=新的简单html\U dom();
//从URL加载HTML
$html->load_文件($url);
//获取所有链接
$anchors=$html->find('li.#Selected a');
//循环遍历每个链接,得到具有“InfoD”类的节点
//每次都要确保清除dom对象以避免内存泄漏
foreach($anchors作为$anchor){
//创建要解析的新链接
$URLTEM=$baseUrl.$anchor->href;
//创建DOM对象
$html2=新的简单html dom();
//从URL加载HTML
$html2->加载_文件($URLTEM);
//获取具有“文本徽标”的所有节点
$div=$html->find('div.InfoD',0);
echo$div;
回声“
”;
//清除dom对象
$html2->clear();
未结算(2美元);
}
//清除dom对象
$html->clear();
未结算($html);
自动跟踪链接并从详细信息页面中获取完整内容的最佳方式是什么?
<div class="InfoD">
<h2>New York</h2>
<h3>USA</h3>
<strong>ITService</strong>
<p>
Tel. : XXXXXX
</p>
<p>
Mail. : XXXX@XXX.com
</p>
</div>
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$url = "http://www.blabla.com/";
$baseUrl= "http://www.blabla.com"
//Create a DOM object
$html = new simple_html_dom();
// Load HTML from a URL
$html->load_file($url);
// Get all links
$anchors = $html->find('li.#Selected a');
// loop through each link and get the node having "InfoD" class
// Everytime make sure to clear dom objects to avoid memory leaks
foreach ($anchors as $anchor) {
// Create the new link to parse
$urlTemp = $baseUrl . $anchor->href;
//Create a DOM object
$html2 = new simple_html_dom();
// Load HTML from a URL
$html2->load_file($urlTemp);
// Get all nodes with "text-logo"
$div = $html->find('div.InfoD', 0);
echo $div;
echo "<hr/>";
// Clear dom object
$html2->clear();
unset($htm2);
}
// Clear dom object
$html->clear();
unset($html);