我有一個腳本,我認爲它是非常基本的抓取,可以稱它爲你,但平均需要至少6秒......是否有可能加速它? $ date變量僅用於計時代碼,並且不會爲其花費的時間添加任何重要內容。我設置了兩個定時標記,每個定時標記之間大約3秒鐘。例如下面的網址進行測試我如何加快速度?
$date = date('m/d/Y h:i:s a', time());
echo "start of timing $date<br /><br />";
include('simple_html_dom.php');
function getUrlAddress()
{
$url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $url .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
$date = date('m/d/Y h:i:s a', time()); echo "<br /><br />after geturl $date<br /><br />";
$parts = explode("/",$url);
$html = file_get_html($url);
$date = date('m/d/Y h:i:s a', time()); echo "<br /><br />after file_get_url $date<br /><br />";
$file_string = file_get_contents($url);
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];
foreach($html->find('img') as $e){
$image = $e->src;
if (preg_match("/orangeBlue/", $image)) { $image = ''; }
if (preg_match("/BeaconSprite/", $image)) { $image = ''; }
if($image != ''){
if (preg_match("/http/", $image)) { $image = $image; }
elseif (preg_match("*//*", $image)) { $image = 'http:'.$image; }
else { $image = $parts['0']."//".$parts[1].$parts[2]."/".$image; }
$size = getimagesize($image);
if (($size[0]>110)&&($size[1]>110)){
if (preg_match("/http/", $image)) { $image = $image; }
echo '<img src='.$image.'><br>';
}
}
}
$date = date('m/d/Y h:i:s a', time()); echo "<br /><br />end of timing $date<br /><br />";
例如URL
UPDATE
這是實際的標記顯示什麼時間:
時間2012年1月24日12時31分50秒開始am
geturl之後01/24/2012 12:31:50 am
時機file_get_url 2012年1月24日上午12點31分53秒
結束後的2012年1月24日上午12時31分57秒
http://www.ebay.co.uk/itm/Duke-Nukem-Forever-XBOX-360-Game-BRAND-NEW-SEALED-UK-PAL-UK-Seller-/170739972246?pt=UK_PC_Video_Games_Video_Games_JS&hash=item27c0e53896`
你在哪裏調用'getUrlAddress()'和/或設置'$ url'變量? – Phil
你有3個時間標記。你能顯示他們輸出的內容嗎?因此,「開始計時」,「geturl之後」,「file_get_url之後」,「計時結束」 –
有一點很突出,那就是你自由使用'preg_match'。有時'strpos'或'strstr'可以更快。正如@Hans所說,'getImageSize()'也是值得關注的東西。 –