2013-07-27 35 views
0

我想放弃使用curlpreg_match一个网站表Preg_match不在内部表中工作?

我的网址http://hosts-file.net/?s=Browse&f=EMD

我卷曲

$url = 'http://hosts-file.net/?s=Browse&f=EMD';  


     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15")); 
     curl_setopt($ch, CURLOPT_NOBODY, false); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     $body= curl_exec ($ch); 
     curl_close ($ch); 

我需要放弃一个表。

preg_match功能如下

preg_match_all('/<table class=\"main_normal(.*?)\">(.*?)<\/table>/s',$body,$vv,PREG_SET_ORDER); 

给出,但它返回空数组只

请指引我

+1

请使用dom解析来抓取html。 – DevZer0

+0

请给我任何例子 –

+0

DOMDocument,DOMXPath,simple_html_dom,phpquery – DevZer0

回答

1

一个例子用DOM文档和DOMXPath:

$doc = new DOMDocument(); 
@$doc->loadHTML($body); 
$xpath = new DOMXPath($doc); 
$links = $xpath->query('/html/body/table/tr/td/table/tr/td/table[@class="main_normal"]/tr/td[2]/a[1]/text()'); 
foreach($links as $link) { 
    echo $link->nodeValue."<br/>"; } 

你可以用相对路径替换第四行,但不如这样不足之处:

$links = $xpath->query('//table[@class="main_normal"]/tr/td[2]/a[1]/text()'); 
+0

谢谢@Casimir et Hippolyte –