douxian5963 2019-02-28 10:47
浏览 78
已采纳

使用PHP解析简单HTML DOM:来自CSV但在数组中的URL只是列表中的最后一个URL

Trying to get all products from specific page with URLs from CSV but it's not working right. There are three arrays:

  1. $test_base
  2. $base - made from the CSV file
  3. $words

It works right just with $test_base, other ones returns empty arrays except last one LINK. And I just don't understand because this LINKS arrays are identical.

include_once('simple_html_dom.php');

// Getting links array
$base = file("base.txt");

$words = array();

foreach($base AS $word) {
    $words[] = $word;
}

$test_base= array("LINK_1","LINK_2","LINK_3");

// Arrays are exactly the same
print_r($test_base);
print_r($base);
print_r($words);

// Main loop for one link
foreach($test_base AS $word) {

     $html = file_get_html($word);

// Getting all info for one link    
foreach($html->find('div.item_info') as $article) {
        $item['title'] = $article->find('.item_name', 0)->plaintext;
        $item['manufacture'] = $article->find('.item_additional-info', 0)->plaintext;
        $item['price'] = $article->find('.price span', 0)->plaintext;

   $articles[] = $item;
}

        echo "<pre>";
        print_r($articles);
        echo "</pre>";

$html->clear();
unset($html);

}
  • 写回答

1条回答 默认 最新

  • dongyou2305 2019-02-28 11:42
    关注

    I have added a FILE_IGNORE_NEW_LINES parameter to the file() function, and its removed a spaces at the end of the string. And now all works like it should. Thanks to all!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?