【问题标题】:Parsing multiple links through a PHP DOM to find classes [closed]通过 PHP DOM 解析多个链接以查找类 [关闭]
【发布时间】:2013-12-18 11:08:22
【问题描述】:

我正在尝试对多个链接使用 DOM 解析器,然后比较 2 对值。有人可以帮我解决我哪里出错了吗?我不能对@class="badge-item-img" 进行比较吗? 编辑我应该提到第一个 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;

【问题讨论】:

  • 如果你不是在寻找设置黑客服务器,我建议尝试使用前端 js 来获得你想要的结果,例如:GreaseMonkey。服务器端html解析html不是很可靠,还有如果目标站点使用js动态填充内容,你的php无论如何也跑不了js。
  • 我喜欢你的名字!我不想经营任何非法网站。我知道我使用的网站不使用 JS 来填充内容。
  • 什么意思 - 比较@class="badge-item-img";您是否尝试查找重复的图像?
  • @ 怎么了?请阅读this answer 以获得更好的方法。
  • 我不知道是谁否决了这个问题,但我不知道为什么。在发布之前我做了很多研究,我相信这是一个 stackoverflow 可以帮助我解决的问题......我是对的。我不在乎你是否不喜欢这个问题......搜索这个网站,这里没有任何东西可以回答我的问题。

标签: php html dom


【解决方案1】:

我修复了这个错误,现在你可以从 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>"
             ;
    }
}

【讨论】:

  • 这里是 $url_array 的定义:$url_array['the_page_path_you_want_to_pull'] = array( 'the_attribute_contains_image_url', 'the_html_dom_xpath', 'the_extra_rule_filter_out_the_address_you_do_not_want/please_remove_here-if_you_do_not_wish_to_filter');
  • +1 充分解释它!谢谢!
【解决方案2】:

您发送的代码似乎缺少以下行:

@$dom2->loadHTML($html);

...我不确定 xPath 搜索,但如果单个实体的 HTML 中有多个类,它也可能有问题 Yes is valid XHTML.

我还建议将 URL 存储在您的第一个循环中,并在您的演示循环中添加演示信息。

foreach($ret as $ret_value) {
  echo '<img style="padding-left:30%" width="500" src="' . $ret_value . '"  alt="" /><br /><br /><br />';
}

【讨论】:

  • 似乎仍然没有加载 2 号的结果。我不能对每个都使用单个,因为我需要先过滤掉结果。
猜你喜欢
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
相关资源
最近更新 更多