使用 Simple HTML Dom Parser 的更好方法
better way with Simple HTML Dom Parser
我的 HTML 代码重复了 16 次:
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
我想获取所有的 imgs 链接和文本也 href 我做了什么:
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
代码变慢了吗?
您使用过多的匿名对象降低了代码速度。这意味着您不要将函数的结果放入变量中,而只是使用它 "on the go"。这需要 运行 你的功能一次又一次地减慢你的项目。
因为你可以使用函数 find
来 return 一个数组,我建议你在 for 循环之前这样做。
$imgarray = $html->find('div[class=headline_image] img', $x);
这样你 运行 $html->find
恰好一次,而不是十六次。在 for 循环中,您可以将其用作数组并处理结果:$imgarray[$x]
。你为 $anchorarray
做同样的事情,你的代码会加速,你会看到。
替代解决方案是在可以找到这 16 项的容器(或正文元素)上使用 PHP DOM $childNodes
。这将 return 十六个 div
元素,您可以通过为 <a>
元素调用 $firstChild
并为 <img>
元素再次调用 $firstChild 来在其中导航。如果您想对网站进行更改(例如在末尾添加更多内容等),这可能更安全
嗨,丹尼尔,我将代码更改为:
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}
一般来说,正确的迭代方式是这样的:
foreach($html->find('div') as $div){
echo $div;
}
我的 HTML 代码重复了 16 次:
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
我想获取所有的 imgs 链接和文本也 href 我做了什么:
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
代码变慢了吗?
您使用过多的匿名对象降低了代码速度。这意味着您不要将函数的结果放入变量中,而只是使用它 "on the go"。这需要 运行 你的功能一次又一次地减慢你的项目。
因为你可以使用函数 find
来 return 一个数组,我建议你在 for 循环之前这样做。
$imgarray = $html->find('div[class=headline_image] img', $x);
这样你 运行 $html->find
恰好一次,而不是十六次。在 for 循环中,您可以将其用作数组并处理结果:$imgarray[$x]
。你为 $anchorarray
做同样的事情,你的代码会加速,你会看到。
替代解决方案是在可以找到这 16 项的容器(或正文元素)上使用 PHP DOM $childNodes
。这将 return 十六个 div
元素,您可以通过为 <a>
元素调用 $firstChild
并为 <img>
元素再次调用 $firstChild 来在其中导航。如果您想对网站进行更改(例如在末尾添加更多内容等),这可能更安全
嗨,丹尼尔,我将代码更改为:
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}
一般来说,正确的迭代方式是这样的:
foreach($html->find('div') as $div){
echo $div;
}