2016-08-14 203 views
0

給定一個頁面的內容(它的HTML),我怎麼能得到文章的內容?獲取文章內容URL

例如,該網站返回給定URL文章內容:

http://embed.ly/docs/explore/extract?url=http%3A%2F%2Fwww.foxnews.com%2Fsports%2F2016%2F08%2F14%2Fryan-lochte-3-other-u-s-swimmers-robbed-in-brazil.html

不過,我並不想用自己的API。我已經使用file_get_contents($url),但我不知道如何去獲取的內容只是文章

任何想法?

+1

你將不得不解析'file_get_contents($ url)'的輸出,並保留你感興趣的部分。 –

+0

如何正則表達式或substr,strstr,strpos,....函數 –

+1

@OrryVandermeulen不,使用內置的解析器。 – chris85

回答

3
$url = 'http://www.foxnews.com/sports/2016/08/14/ryan-lochte-3-other-u-s-swimmers-robbed-in-brazil.html'; 
$content = file_get_contents($url); 
$first_step = explode('<div class="article-text">' , $content); 
$paras = explode("<p>" , $first_step[1]); 

foreach($paras as $para) { 
    echo $para; 
} 

在這裏,如果你想獲得圖像的內容也使用文章標籤在他們的dom結構中使用。

+1

希望'article-text'裏面沒有'div'。 – chris85