0

(ちなみに、問題のWebサイトの許可を得て、このようなものをスクレイピングしています)。

非常に単純な Web スクレイパーで、手動ですべてのリンクを読み込んでいたときは問題なく動作していましたが、JSON と変数を介して読み込もうとしたとき (そのため、1 つのスクリプトで多くのスクレイピングを行い、プロセスをよりモジュール化することができます) JSON へのリンクを追加するだけで) 無限ループで実行されます。

(ページの読み込みに約 15 分かかります)

これが私のJSONです。テスト目的でそこにあるのは 1 店舗だけですが、約 15 店舗が追加される予定です。

[
   {
      "store":"Incu Men",
      "cat":"Accessories",
      "general_cat":"Accessories",
      "spec_cat":"accessories",
      "url":"http://www.incuclothing.com/shop-men/accessories/",
      "baseurl":"http://www.incuclothing.com",
      "next_select":"a.next",
      "prod_name_select":".infobox .fn",
      "label_name_select":".infobox .brand",
      "desc_select":".infobox .description",
      "price_select":"#price",
      "mainImg_select":"",
      "more_imgs":".product-images",
      "product_url":".hproduct .photo-link"
   }
]

PHPスクレーパーコードは次のとおりです。

<?php
//Set infinite time limit
set_time_limit (0);
// Include simple html dom
include('simple_html_dom.php');
// Defining the basic cURL function
function curl($url) {
  $ch = curl_init();
    // Initialising cURL
    curl_setopt($ch, CURLOPT_URL, $url);
    // Setting cURL's URL option with the $url variable passed into the function
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Setting cURL's option to return the webpage data
    $data = curl_exec($ch);
    // Executing the cURL request and assigning the returned data to the $data variable
    curl_close($ch);
    // Closing cURL
    return $data;
    // Returning the data from the function
}

function getLinks($catURL, $prodURL, $baseURL, $next_select) {
    $urls = array();

    while($catURL) {
        echo "Indexing: $url" . PHP_EOL;
        $html = str_get_html(curl($catURL));

        foreach ($html->find($prodURL) as $el) {
            $urls[] = $baseURL . $el->href;
        }

        $next = $html->find($next_select, 0);
        $url = $next ? $baseURL . $next->href : null;

        echo "Results: $next" . PHP_EOL;
    }

    return $urls;
}

$string     = file_get_contents("jsonWorkers/incuMens.json");
$json_array = json_decode($string,true);

foreach ($json_array as $value){

    $baseURL = $value['baseurl'];
    $catURL = $value['url'];
    $store = $value['store'];
    $general_cat = $value['general_cat'];
    $spec_cat = $value['spec_cat'];
    $next_select = $value['next_select'];
    $prod_name = $value['prod_name_select'];
    $label_name = $value['label_name_select'];
    $description = $value['desc_select'];
    $price = $value['price_select'];
    $prodURL = $value['product_url'];

    if (!is_null($value['mainImg_select'])){
        $mainImg = $value['mainImg_select'];
    }
    $more_imgs = $value['more_imgs'];



    $allLinks = getLinks($catURL, $prodURL, $baseURL, $next_select);

}

?>

スクリプトが無限に実行され、何も返さない/停止/画面に何かを印刷しない理由はありますか? 止まるまで放っておきます。これを手作業で行っていたときは、1分ほどしかかからず、場合によってはそれ以下だったので、変数/ jsonに問題があると確信していますが、問題が何であるかを一生見ることはできません.

誰でも簡単に見て、正しい方向に向けることができますか?

4

1 に答える 1

3

while($catURL)ループに問題があります。何をしたいですか ?さらに、コマンドを使用してブラウザに情報を表示するよう強制することもできますflush()

于 2013-05-16T06:19:13.907 に答える