Home  >  Article  >  Backend Development  >  php文章内容抓取解决方法

php文章内容抓取解决方法

WBOY
WBOYOriginal
2016-06-13 12:04:37899browse

php文章内容抓取

本帖最后由 yanfangphp 于 2014-08-13 10:14:37 编辑 求大神帮忙抓取这个网页http://sports.sohu.com/zhongchao.shtml的排行榜部分的数据(包括积分榜和射手榜)



------解决方案--------------------
$url = 'http://sports.sohu.com/zhongchao.shtml';<br />$s = file_get_contents($url);<br />preg_match_all('/(?<=<div class="turn cons">)\s<table.+table>/isU', $s, $m);<br />print_r(preg_grep('/名次/', $m[0]));
Array<br />(<br />    [2] => <br /><table border=0 cellSpacing=0 cellPadding=0 width="100%"><br /><tbody><br /><tr><br /><th width="15%">名次</th><br /><th width="47%">球队</th><br /><th width="9%">场次</th><br /><th width="29%">积分</th></tr><br /><tr><br /><td>01</td><br /><td><a href="http://sports.sohu.com/s2010/7742/s277701524/" target="_blank">广州恒大</a></td><br /><td>20</td><br /><td>45</td><br /></tr><br /><tr><br /><td>02</td><br /><td><a href="http://sports.sohu.com/s2006/7742/s242155493/" target="_blank">北京国安</a></td><br />......
接下来自己做
------解决方案--------------------
给你推荐个类  simple_html_dom

<br />include "simple_html_dom.class.php";<br /><br />$url = "http://sports.sohu.com/zhongchao.shtml";<br />$dom = new simple_html_dom();<br />$html = $dom->load(file_get_contents($url));<br /><br />$res = $html->find("div#turnIDB div.turn");<br /># 积分榜<br />echo $res[0]->outertext;<br /># 射手榜<br />echo $res[1]->outertext;<br />


结果

------解决方案--------------------
$str=file_get_contents("http://sports.sohu.com/zhongchao.shtml");<br /><br />preg_match_all('/<tr>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(\d+)<\/td>\s*<td>(.+?)<\/td>\s*<\/tr>/i',$str,$match1);<br /><br />foreach($match1 as $k=>$v){<br />	if($k!=0){<br />		foreach($v as $k1=>$v1){<br />			if($k1<=15){<br />				$jifen[$k][]=$v1;<br />			}else{<br />				$sheshou[$k][]=$v1;<br />			}<br />		}<br />	}<br />}<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($jifen);<br />print_r($sheshou);<br />echo "
";
/*
Array
(
    [1] => Array
        (
            [0] => 01
            [1] => 02
            [2] => 03
            [3] => 04
            [4] => 05
            [5] => 06
            [6] => 07
            [7] => 08
            [8] => 09
            [9] => 10
            [10] => 11
            [11] => 12
            [12] => 13
            [13] => 14
            [14] => 15
            [15] => 16
        )

    [2] => Array
        (
            [0] => 广州恒大
            [1] => 北京国安
            [2] => 广州富力
            [3] => 上海东亚
            [4] => 贵州茅台
            [5] => 山东鲁能
            [6] => 天津泰达
            [7] => 江苏舜天
            [8] => 上海绿地
            [9] => 长春亚泰
            [10] => 杭州绿城
            [11] => 大连阿尔滨
            [12] => 上海申鑫
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn