如何使用for循环使用php从url列表中提取数据


how to use the for loop to extract data from a list of url using php

下面代码中的for循环无法正常工作。

 $html= @file_get_html($url);
 $job_array = array();
 foreach($html->find('a') as $link) {
  // $links=$html->find('a');
  if (strpos($link->href, '/job-category/') !== false) {
   $job_array[] = $link->href . "<br/>";
  }
  for ($a = 0; $a <= ($link->href); $a++) {
   //$page_number = 20;
   // for ($i = 1; $i <= $page_number; $i++) {
   $html2 = file_get_html($link->href);
   $response = array();
   foreach ($html2->find('div#mainContent') as $header) {
    $response[] = $header->innertext . "<br/>";
    print_r($response);
   }
  }

我认为

$link->href

不是数字,for循环不能使用非数字来比较$a和进行迭代。也许你可以做:

    $html= @file_get_html($url); 
    $job_array = array();
    $myNumberToIterateWith = 0;
      foreach($html->find('a') as $link) { 
      // $links=$html->find('a');
        $myNumberToIterateWith++; 
        if (strpos($link->href, '/job-category/') !== false) { 
           $job_array[] = $link->href . " ";
        }
      for ($a = 0; $a <= $myNumberToIterateWith; $a++) {
       //$page_number = 20;
       // for ($i = 1; $i <= $page_number; $i++) {
       $html2 = file_get_html($link->href);
       $response = array();
       foreach ($html2->find('div#mainContent') as $header) {
        $response[] = $header->innertext . "<br/>";
        print_r($response);
       }
      }

虽然我不确定你希望结果是什么。提供你希望用代码实现什么的线索是很有帮助的。