我正在尝试将DOM解析器用于多个链接,然后比较2对值。有人可以帮助我解决我的错误吗?我不可能对@class="badge-item-img"
进行比较吗?
编辑我应该提到第一个foreach有效但是当试图找到第二个foreach时没有显示结果。
<?php
// Init the '$url_array' array.
$url_array = array();
$url_array[] = 'http://www.reddit.com/r/funny';
$url_array[] = 'http://www.9gag.com/';
// Init the return '$ret' array.
$ret = array();
// Roll through the '$url_array' array.
foreach ($url_array as $url_value) {
$html = file_get_contents($url_value);
$dom = new DOMDocument();
$dom2 = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$xpath2 = new DOMXPath($dom2);
$hyperlinks = $xpath->evaluate('//a[@class="thumbnail "]');
$hyperlinks2 = $xpath2->evaluate('//a[@class="badge-item-img"]');
foreach($hyperlinks as $hyperlink) {
if(strpos($hyperlink->getAttribute('href'), 'http://i.imgur.com/') !== FALSE){
$ret[] = "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlink->getAttribute('href') . "\" alt=\"\" />"
. "<br>"
. "<br>"
. "<br>"
;
}
foreach($hyperlinks2 as $hyperlinker) {
$ret[] = "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlinker->getAttribute('href') . "\" alt=\"\" />"
. "<br>"
. "<br>"
. "<br>"
;
}
}
}
// Roll through the '$ret' array.
foreach($ret as $ret_value) {
echo $ret_value;
答案 0 :(得分:0)
您发送的代码似乎缺少以下一行:
@$dom2->loadHTML($html);
...我不确定xPath搜索,但如果单个实体的HTML中有多个类,则可能也会遇到问题。是的是有效的XHTML。
我还建议您将URL存储在第一个循环中,并在演示循环中添加演示文稿信息。
foreach($ret as $ret_value) {
echo '<img style="padding-left:30%" width="500" src="' . $ret_value . '" alt="" /><br /><br /><br />';
}
答案 1 :(得分:0)
我修复了这个错误,现在你可以从9gag
中提取图片了<?php
// Init the '$url_array' array.
$url_array = array();
$url_array['http://www.reddit.com/r/funny'] = array( 'href', '//a[@class="thumbnail "]', 'http://i.imgur.com/');
$url_array['http://www.9gag.com/'] = array( 'src', '//img[@class="badge-item-img"]' );
// Init the return '$ret' array.
$ret = array();
// Roll through the '$url_array' array.
foreach ($url_array as $url_value => $ary_rules) {
$html = file_get_contents($url_value);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$hyperlinks = $xpath->evaluate($ary_rules[1]);
foreach($hyperlinks as $hyperlink) {
if( !$ary_rules[2] || strpos($hyperlink->getAttribute($ary_rules[0]), $ary_rules[2] ) !== FALSE){
$ret[$url_value][] = $hyperlink->getAttribute($ary_rules[0]);
}
}
}
// Roll through the '$ret' array.
foreach($ret as $ret_value_list) {
foreach($ret_value_list as $ret_value){
echo "<img style='padding-left:30%' width=\"500\" src=\"" . $ret_value . "\" alt=\"\" />"
. "<br>"
. "<br>"
. "<br>"
;
}
}