2016-08-14 150 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