Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将xpath与for而不是foreach一起使用_Php_Xpath - Fatal编程技术网

Php 如何将xpath与for而不是foreach一起使用

Php 如何将xpath与for而不是foreach一起使用,php,xpath,Php,Xpath,我想刮有3级刮网站。首先,我得到所有页面,然后在每个页面中,我得到图像、标题、url,这些信息将我重定向到一个独特的页面,该页面包含更多信息,如描述、日期等。因此,如果我使用foreach它会给出错误的结果,如果我使用for而不是foreach它只返回一个对象。如何处理if(使用for而不是foreach) 您是否收到任何错误?检查日志文件。当您尝试为DOMXPath查询结果编制索引时,我立即看到了一个错误:$pageLinks[$a],该错误应为:PHP致命错误:无法将DOMNodeList类

我想刮有3级刮网站。首先,我得到所有页面,然后在每个页面中,我得到图像、标题、url,这些信息将我重定向到一个独特的页面,该页面包含更多信息,如描述、日期等。因此,如果我使用
foreach
它会给出错误的结果,如果我使用
for
而不是
foreach
它只返回一个对象。如何处理if(使用
for
而不是
foreach


您是否收到任何错误?检查日志文件。当您尝试为DOMXPath查询结果编制索引时,我立即看到了一个错误:
$pageLinks[$a]
,该错误应为:
PHP致命错误:无法将DOMNodeList类型的对象用作数组
@Parfait,不,我没有收到任何错误。对于第一个数组,我的意思是如果
$a=0
它返回正确的值,当
$a
增加时,title、pagelink、img也有其正确的值,尽管description、date、vention没有启动。这很有趣!我从字面上复制了您的整个代码,并在我这端运行了它,在第61行中出错,指向
$pageLinks[$a]
的消息与上面相同。好奇的是,您正在运行哪个版本的PHP?也许PHP7解决了这个问题。我使用PHP5.4。@Parfait。我使用phpDo的5.6.11,您是否打开了错误报告?检查。您是否收到任何错误?检查日志文件。当您尝试为DOMXPath查询结果编制索引时,我立即看到了一个错误:
$pageLinks[$a]
,该错误应为:
PHP致命错误:无法将DOMNodeList类型的对象用作数组
@Parfait,不,我没有收到任何错误。对于第一个数组,我的意思是如果
$a=0
它返回正确的值,当
$a
增加时,title、pagelink、img也有其正确的值,尽管description、date、vention没有启动。这很有趣!我从字面上复制了您的整个代码,并在我这端运行了它,在第61行中出错,指向
$pageLinks[$a]
的消息与上面相同。好奇的是,您正在运行哪个版本的PHP?也许PHP7解决了这个问题。我使用PHP5.4。@Parfait。我使用phpDo的5.6.11,您是否打开了错误报告?检查。
<?php 
   $stackHref=array();
   $eventDetail=array();

$sitecontent =  file_get_contents('https://www.everfest.com/music/edm-festivals');


    if($sitecontent === FALSE) {

        $error_log .= 'Error on  $sitecontent =  file_get_contents(https://www.everfest.com/music/edm-festivals) ';
        //insert_error($error_log);
    }
   //  echo $sitecontent;
   $dom = new DOMDocument();
   libxml_use_internal_errors(true);
   $dom->loadHTML($sitecontent);
   libxml_use_internal_errors(false);
   $xpath = new DOMXPath($dom);     
   $nodes = $xpath->query("(//ul[@class='pagination'])[1]/li/a/@href");
   // $all_area_set= ' ';
   //echo $sitecontent;
  if(!isset($nodes))
  {
       $error_log .= "Error on $nodes = $xpath->query((//ul[@class='pagination'])[1]/li/a/@href)";  
       //insert_error($error_log);
       echo $error_log;
  }

  // get total pages

  foreach ($nodes as $link) {
      $stackHref[]='https://www.everfest.com'.$link->nodeValue;      
  }

  //loop through each pages in order to scrape 
  $j=0;
  for($i=0;$i<count($stackHref);$i++){

      $sitecontent=file_get_contents($stackHref[$i]);

      if($sitecontent === FALSE) {

        $error_log .= 'Error on  $sitecontent =  file_get_contents(https://www.everfest.com/music/edm-festivals) ';
        //insert_error($error_log);
      }

      $dom= new DOMDocument();
      libxml_use_internal_errors(TRUE);
      $dom->loadHTML($sitecontent);
      libxml_use_internal_errors(FALSE);
      $innerXpath= new DOMXPath($dom);



              //get page link

              $pageLinks= $innerXpath->query('//div[@class="festival-card grow"]/a[1]/@href');

                  for ($a=0;$a <$pageLinks->length;$a++ ){
                      //get img src
                        $eventDetail[$j]['pagelink']='https://www.everfest.com'.$pageLinks[$a]->nodeValue;
                        $images= $innerXpath->query("//div[contains(@class,'columns medium-6 large-4')]/div[contains(@class,'grow')]/a/img/@src");
                      $eventDetail[$j]['img']=$images[$a]->nodeValue;
                          //get title
                           $titles= $innerXpath->query("//div[contains(@class,'clearfix')]/a[1]/text()");
                              $eventDetail[$j]['title']=$titles[$a]->nodeValue;

                                      // go inside of each pages in order to get description, date, venue
                                      $sitecontent=file_get_contents($eventDetail[$j]['pagelink']);
                                      $dom= new DOMDocument();
                                      libxml_use_internal_errors(TRUE);
                                      $dom->loadHTML($sitecontent);
                                      libxml_use_internal_errors(FALSE);
                                      $deepxpath= new DOMXPath($dom);
                                      $descriptions= $deepxpath->query('//div[@class="columns"]/div[contains(@class,"card-white")]/p[contains(@class,"")]/span[1]/following-sibling::text()[1]');

                                          $eventDetail[$j]['description']=$descriptions[$a]->nodeValue;

                                      //get date
                                      $dates= $deepxpath->query('//div[@id="signup"]/div[@class="row"]/div[contains(@class,"columns")][1]/p/text()[1]');

                                          $eventDetail[$j]['Date']=$dates[$a]->nodeValue;

                                      //get venue
                                      $venues= $deepxpath->query('//div[@id="signup"]/div[@class="row"]/div[contains(@class,"columns")][1]/p/text()[2]');

                                          $eventDetail[$j++]['venue']=$venues[$a]->nodeValue;



                                      }
                                      }     
?>