Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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文件_get _html存在奇怪的问题_Php_Screen Scraping - Fatal编程技术网

PHP文件_get _html存在奇怪的问题

PHP文件_get _html存在奇怪的问题,php,screen-scraping,Php,Screen Scraping,嗨,我正在尝试使用simple_html_dom进行文本网站集群项目,但我遇到了一个奇怪的问题。当我在外循环中使用echo时,url和代码段是您所期望的,但是当我尝试回显我在外循环中收集的数组内容时,url是正常的,但是代码段已经消失,最后一个代码段已经就位 <?php // create HTML DOM include("simple_html_dom.php"); $search_query = 'something'; $j = 1; $k = 1; /**

嗨,我正在尝试使用simple_html_dom进行文本网站集群项目,但我遇到了一个奇怪的问题。当我在外循环中使用echo时,url和代码段是您所期望的,但是当我尝试回显我在外循环中收集的数组内容时,url是正常的,但是代码段已经消失,最后一个代码段已经就位

<?php
  // create HTML DOM
  include("simple_html_dom.php");
  $search_query = 'something';
  $j = 1;
  $k = 1;
  /*************************GOOGLE***************************/
  for ($i = 0; $i < 1; $i++) {
      $url = sprintf('http://www.google.com/search?q=%s&start=%d', $search_query, 10 * $i);
      $html = file_get_html($url);
      foreach ($html->find('a[class=l]') as $element) {
          $urls[$j] = $element->href;
          echo $element->href . "\n\n\n\n\n";
          $j++;
      }
      foreach ($html->find('div[class=s]') as $element) {
          $snippets[$k] = $element->innertext;
          echo $element->innertext . "\n\n\n\n\n";
          $k++;
      }
  }
  $j = 1;
  foreach ($snippets as $elemement) {
      echo $urls[$j] . "\n" . $element . "\n\n\n\n";
      $j++;
  }
?>
你打错了,$elemenent应该是$element


这是习惯于编写可读代码的一个原因。这并不是因为其他人喜欢它,而是因为它使调试更加容易。

您确定您的代码中没有输入错误吗

foreach ($snippets as $elemement) {
      echo $urls[$j] . "\n" . $element . "\n\n\n\n";
      $j++;
  }

元素和元素是不同的;您的循环执行得很好,但您的语句可能不会。请下次发布可读代码,您可以使用。哈哈,谢谢!我通常在阅读大量无法阅读的代码时不会遇到问题,但我现在真的需要睡觉。是的,谢谢。这对你们所有人来说都很明显,但我花了30分钟看它,…必须睡觉!
foreach ($snippets as $elemement) {
      echo $urls[$j] . "\n" . $element . "\n\n\n\n";
      $j++;
  }