我想解析2个使用PHP DOM的网址。我想知道是否有人能够向我提供有关我出错地点的信息?是否无法像我在下面所做的那样解析这两个URL?
<?php
$html = file_get_contents('http://www.reddit.com/r/funny');
$html2 = file_get_contents('http://www.9gag.com/');
$dom = new DOMDocument();
@$dom->loadHTML($html.$html2);
$xpath = new DOMXPath($dom);
$hyperlinks = $xpath->evaluate('//a[@class="thumbnail "]');
$hyperlinks2 = $xpath->evaluate('//a[@class="badge-item-img"]');
foreach($hyperlinks as $hyperlink) {
if(strpos($hyperlink->getAttribute('href'), 'http://i.imgur.com/') !== FALSE){
echo "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlink->getAttribute('href') . "\" alt=\"\" />";
echo "<br>";
echo "<br>";
echo "<br>";
}
else{
echo "";
}
}
?>
编辑此编辑已作为我试图做比较的类=“徽章项-IMG”,它不是返回任何东西。难道我没有这样做吗?
<?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;
}
看到我上面的编辑...它进一步解释了我的意思是这个问题。我在编辑中实施了您的解决方案。 –