2012-05-03 53 views
0

我需要證明被搜查由谷歌THROU simple_html_dom.php使用SimpleHTMLDom獲得大圖片THROU谷歌搜索

include_once("simple_html_dom.php"); 
     $key="alexis"; 
     $html = new simple_html_dom(); 
     $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y'); 
foreach($html->find('a') as $element) { 
preg_match('#(?:http://)?(http(s?)://([^\s]*)\.(jpg|gif|png))#', $element->href, $imagelink); 
echo $imagelink['1']; 
} 

結果有多大的圖像:

http://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_4.jpghttp://liberalvaluesblog.com/wp-content/uploads/2009/03/alexis-bledel-gg02.jpghttp://cdn0.sbnation.com/imported_assets/119825/alexis.jpghttp://www.hdwallpapersarena.com/wp-content/uploads/2012/04/alexis-dziena-3163.jpghttp://www.nba.com/media/celtics/alexis-diary300422.jpghttp://beautiful-pics.org/wp-content/uploads/2011/06/Alexis_Bledel_5.jpghttp://www.popcrunch.com/wp-content/uploads/2009/03/amanda-alexis-lee2.jpghttp://www.tvfunspot.com/forums/images/realityshows/survivor16/alexis.jpghttp://access.nscpcdn.com/gallery/i/b/bledel/AlexisBlei_4705661.jpghttp://2.bp.blogspot.com/_UaLWp72nij4/S_cccrvmFsI/AAAAAAAAMM8/yjVXCl3sHT4/s1600/alexis-bledel.jpghttp://3.bp.blogspot.com/-RFJoXjDSnmQ/T4cPwZwgrjI/AAAAAAAABlY/aNlgZ3ZFknU/s1600/Alexis%252BBledel-03.jpghttp://images2.fanpop.com/images/photos/4200000/Alexis-Bledel-alexis-bledel-4218179-590-768.jpghttp://www.hawktime.com/sitebuildercontent/sitebuilderpictures/AlexisArguello.jpghttp://4.bp.blogspot.com/--iAuASq2RFc/TveqvJ2j8vI/AAAAAAAABvg/RzTmjASqK_0/s400/alexis-bledel.jpghttp://www.mtv.com/news/photos/a/american_idol/09/top36/10_alexis_grace.jpghttp://images.buddytv.com/articles/the-bachelor/images/alexis.jpghttp://cdn01.cdnwp.thefrisky.com/wp-content/uploads/2010/02/03/vanity-fair-020310-main.jpghttp://www.chorva.net/wp-content/uploads/2009/04/alexis-bledel.jpghttp://images2.fanpop.com/images/photos/3500000/Alexis-Bledel-alexis-bledel-3523783-454-600.jpghttp://www.pocandpoch.com/wp-content/gallery/alexis-and-lauren/alexis-bledel.jpg

但我需要一個鏈接只爲第一個圖像,謝謝

回答

0
include_once("simple_html_dom.php"); 
     $key="alexis"; 
     $html = new simple_html_dom(); 
     $html=file_get_html('http://images.google.com/images?as_q='. $key .'&hl=en&imgtbs=z&btnG=Search+Images&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y'); 
foreach($html->find('a') as $element) { 
if(preg_match('#(?:http://)?(http(s?)://([^\s]*)\.(jpg|gif|png))#', $element->href, $imagelink)){ 
    echo $imagelink[1]; 
    break; 
} 
} 
+0

非常感謝!它的工作! – devcline

0

add a break;後與echo行:

... 
echo $imagelink['1']; 
break; 
}