简单 HTML DOM 查找标签并从页面 link 获取数据
Simple HTML DOM find tags and fetch data from page link
简单 HTML DOM 查找标签并从页面获取数据 link
嗨,我很简单 HTML DOM,基本上我需要从中获取 h2 标题和内容
links (page/id/1)。我得到堆栈的要点是从 page 获取数据。
格式应与
相同
- 标题
contet 形式 lik1 ,
来自 link5
的内容
- 标题 2
来自 link 的内容,
来自 2
的内容
<section class="level">
<h2> title </h2>
<a class="links" href="page/id/1">link1 </a>
<a class="links" href="page/id/2">link2 </a>
<a class="links" href="page/id/3">link3 </a>
<a class="links" href="page/id/4">link4 </a>
<a class="links" href="page/id/5">link5 </a>
</section>
<section class="level">
<h2> title 2 </h2>
<a class="links" href="page/id/7">link1 </a>
<a class="links" href="page/id/8">link2 </a>
</section>
<section class="level">
<h2> title 3 </h2>
<a class="links" href="page/id/9">link2 </a>
<a class="links" href="page/id/10">link3 </a>
</section>
我知道应该按照这些思路帮助大家
foreach ($html->find('h2') as $key => $value) {
echo $html->find('h2',0)->plaintext;
//this is where Im stack getting the data from the link
foreach ( ) {
echo data from the link example.com/page.php/id/1
echo data from the link example.com/page.php/id/2
}
}
您可以使用 find('section[class=level]')
find 具有类名 level
的 <section>
然后您可以循环子节点并检查节点名称。
要仅获取锚点,您可以使用 find('section[class=level] a')
例如:
$html = new simple_html_dom();
$html->load($data);
$result = $html->find('section[class=level]');
foreach ($result as $item) {
foreach($item->childNodes() as $childNode) {
if ($childNode->nodeName() === "h2") {
echo $childNode->innertext . "<br>";
}
if ($childNode->nodeName() === "a") {
echo $childNode->getAttribute("href") . "<br>";
}
}
}
结果
title
page/id/1
page/id/2
page/id/3
page/id/4
page/id/5
title 2
page/id/7
page/id/8
title 3
page/id/9
page/id/10
简单 HTML DOM 查找标签并从页面获取数据 link 嗨,我很简单 HTML DOM,基本上我需要从中获取 h2 标题和内容 links (page/id/1)。我得到堆栈的要点是从 page 获取数据。 格式应与
相同- 标题
contet 形式 lik1 , 来自 link5
的内容- 标题 2
来自 link 的内容, 来自 2
的内容 <section class="level">
<h2> title </h2>
<a class="links" href="page/id/1">link1 </a>
<a class="links" href="page/id/2">link2 </a>
<a class="links" href="page/id/3">link3 </a>
<a class="links" href="page/id/4">link4 </a>
<a class="links" href="page/id/5">link5 </a>
</section>
<section class="level">
<h2> title 2 </h2>
<a class="links" href="page/id/7">link1 </a>
<a class="links" href="page/id/8">link2 </a>
</section>
<section class="level">
<h2> title 3 </h2>
<a class="links" href="page/id/9">link2 </a>
<a class="links" href="page/id/10">link3 </a>
</section>
我知道应该按照这些思路帮助大家
foreach ($html->find('h2') as $key => $value) {
echo $html->find('h2',0)->plaintext;
//this is where Im stack getting the data from the link
foreach ( ) {
echo data from the link example.com/page.php/id/1
echo data from the link example.com/page.php/id/2
}
}
您可以使用 find('section[class=level]')
find 具有类名 level
的 <section>
然后您可以循环子节点并检查节点名称。
要仅获取锚点,您可以使用 find('section[class=level] a')
例如:
$html = new simple_html_dom();
$html->load($data);
$result = $html->find('section[class=level]');
foreach ($result as $item) {
foreach($item->childNodes() as $childNode) {
if ($childNode->nodeName() === "h2") {
echo $childNode->innertext . "<br>";
}
if ($childNode->nodeName() === "a") {
echo $childNode->getAttribute("href") . "<br>";
}
}
}
结果
title
page/id/1
page/id/2
page/id/3
page/id/4
page/id/5
title 2
page/id/7
page/id/8
title 3
page/id/9
page/id/10