簡體   English   中英

PHP腳本應花費6個小時,但在30分鍾后停止

[英]PHP script supposed to take 6 hours but stops after 30 minutes

我已經制作了一個基本的Web爬網程序來從網站上抓取信息,我估計大約需要6個小時(將頁面數乘以獲取信息所需的時間),但是在循環瀏覽大約30-40分鍾后我的功能停止運行,而我只得到了所需信息的一小部分。 工作時,頁面看起來像它的裝載和它輸出的地方是到屏幕上,但是當它停止,頁面加載停止和輸入停止顯示。

無論如何,我可以保持頁面加載,而不必每30分鍾重新啟動一次嗎?

編輯:這是我的代碼

function scrape_ingredients($recipe_url, $recipe_title, $recipe_number, $this_count) {
    $page   = file_get_contents($recipe_url);

    $edited = str_replace("<h2 class=\"ingredients\">", "<h2 class=\"ingredients\"><h2>", $page);

    $split  = explode("<h2 class=\"ingredients\">", $edited);
    preg_match("/<div[^>]*class=\"module-content\">(.*?)<\\/div>/si", $split[1], $ingredients);

    $ingred = str_replace("<ul>", "", $ingredients[1]);
    $ingred = str_replace("</ul>", "", $ingred);
    $ingred = str_replace("<li>", "", $ingred);
    $ingred = str_replace("</li>", ", ", $ingred);

    echo $ingred;
    mysql_query("INSERT INTO food_tags (title, link, ingredients) VALUES ('$recipe_title', '$recipe_url', '$ingred')");

    echo "<br><br>Recipes indexed: $recipe_number<hr><br><br>";

}

$get_urls   = mysql_query("SELECT * FROM food_recipes WHERE id>3091");
while($row  = mysql_fetch_array($get_urls)) {
    $count++;
    $thiscount++;
    scrape_ingredients($row['link'], $row['title'], $count, $thiscount);

    sleep(1);
}

嘗試添加

set_time_limit(0);

在腳本的頂部。

您的php.ini的set_time_limit選項值是多少? 必須將其設置為0,腳本才能無限運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM