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!

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

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题