2016-02-23 350 views
1

我使用Simple HTML DOM來抓取某些網站的鏈接,但是我遇到了許多網站使用相對鏈接而不是完整URL的問題。更改抓取鏈接的基本URL

所以會發生什麼是我抓取的鏈接,並將其輸出直接到我的網站,但每一個環節導致www.mydomain.com/somearticle而不是www.crawleddomain.com/somearticle

我已經做了一些挖掘,我發現了關於BASE tag。由於我從多個網站進行抓取,因此我不能爲我的網站設置基本標記,因爲它會從輸出更改爲輸出。所以我正在尋找只有某個div的基礎標籤。我偶然發現this answer

不過,我試過手動包括基本網址,如下所示,但沒有奏效:

echo ('http://www.baselink.com/' . strip_tags($post, '<p><a>')); 

我也嘗試了第二個選項,與correct_urls($html, $baseurl);功能,但顯然不存在。

有什麼方法可以將基本URL(或追加它)更改爲PHP中的for-loop中的相對URL?

Here is the output

這裏是我使用的代碼:

<div class='rcorners1'> 
<?php 
include_once('simple_html_dom.php'); 

$target_url = "http://www.buzzfeed.com/trending?country=en-us"; 

$html = new simple_html_dom(); 

$html->load_file($target_url); 

$posts = $html->find('ul[class=list--numbered trending-posts trending-posts-now]'); 
$limit = 10; 
$limit = count($posts) < $limit ? count($posts) : $limit; 
for($i=0; $i < $limit; $i++){ 
    $post = $posts[$i]; 
    $post->find('div[class=trending-post-text]',0)->outertext = ""; 
    echo strip_tags ($post, '<p><a>'); 
} 
?> 
</div> 
</div> 

回答