2017-02-12 247 views
1

我想提取的HTML錶鏈接和日期/廢料郵編鏈接,並從以下鏈接的發佈選項卡上相應的日期:網頁抓取從3GPP網址

3GPP report Website

我可以使用下面的PHP代碼中提取郵編鏈接:

preg_match_all('/<ul class=\"rpRootGroup\">(.*?)<\/ul/s',$specpage,$zipul); 
$specul = new domDocument; 
@$specul->loadHTML($zipul[0][0]); 
$specul->preserveWhiteSpace = true; 
$xpathspecul = new DOMXPath($specul); 
$rowsUL = $xpathspecul->query('//tr'); 
$resultul = array(); 
$zipf = array(); 
$zipuni = array(); 

foreach ($rowsUL as $rowul) { 
    $colsul = $rowul->getElementsByTagName('td'); 
    foreach ($colsul as $colul) { 

     if($xpathspecul->evaluate('count(.//a)', $colul) > 0) { // check if an anchor exists 
      $slinkul = $xpathspecul->evaluate('string(.//a/@href)', $colul); // if there is, then echo the href value 
     } 
     if (isset($slinkul) && $slinkul!=null){ 
      $resultul[] = $slinkul; 
     } 
    } 
} 

foreach ($resultul as $ziplink){ 
    $chkzip = pathinfo($ziplink, PATHINFO_EXTENSION); 
    if ($chkzip == 'zip' && $ziplink!==null){ 
     $zipf[] = trim($ziplink); 
    } 
} 
$zipuni = array_values (array_unique($zipf)); 

$ specpage包含使用curl

Sample image of aforementioned Zip link and Date

但是加載的網站,我不能夠提取相應的日期。

此外,我有使用'array_unique'的問題,因爲可以有相同的Zip鏈接,但具有不同的相應日期。但是,如果沒有'array_unique'即時通訊獲得很多多個鏈接。

任何幫助表示讚賞。

回答

2

如果你的字面意思只是試圖從給定的頁面抓取日期(00-00-0000)和zip url,那麼你可以在下面使用它。你可以很容易地把它放到一個正則表達式中,但更清楚地看到它是如何使用兩個的。由於Regex查詢如此特殊,我每次查詢只准確地進行了21次匹配,因此只需使用鍵創建附加數組,以便輕鬆排序數據。

$url = 'https://portal.3gpp.org/desktopmodules/Specifications/SpecificationDetails.aspx?specificationId=1387'; 
$data = file_get_contents($url); 
preg_match_all('/http:\/\/.*\.zip/', $data, $links); 
preg_match_all('/<\/td><td>\s*(\d*-\d*-\d*)\s*<\/td><td>/', $data, $dates); 
$newArr = []; //Your new array with URL and Dates 

foreach($dates[0] as $k=>$v) { 

    $newArr[] = ['date' => $v, 'url' => $links[0][$k]]; 
    echo 'Date: ' . $newArr[$k]['date'] . '<br>URL: ' . $newArr[$k]['url'] . '<br><br>'; 
    //echo is for testing purposes. 
} 

輸出:

Date: 2015-12-18 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-d00.zip 

Date: 2014-09-26 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-c00.zip 

Date: 2012-09-21 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-b00.zip 

Date: 2011-04-05 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-a00.zip 

Date: 2009-12-18 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-900.zip 

Date: 2008-12-18 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-800.zip 

Date: 2007-06-21 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-700.zip 

Date: 2005-01-06 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-600.zip 

Date: 2004-04-01 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-530.zip 

Date: 2003-10-02 
URL: http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-520.zip 

etc.... 

我已經當場檢查數據和日期匹配完美地與聯繫。

+1

太棒了!它效果很好。非常感謝你 – user2034593

+0

非常好。我不知道他想從頁面中提取什麼。我無法破譯圖像。 XML不起作用的原因是HTML標記錯誤太多。所以我爲33個表中的每一個隔離了HTML。即使這些表格也有太多HTML標記錯誤可供XML使用。 – Misunderstood