Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php文章内容抓取

php文章内容抓取

Jun 23, 2016 pm 01:51 PM
php crawl Article content

求大神帮忙抓取这个网页http://sports.sohu.com/zhongchao.shtml的排行榜部分的数据(包括积分榜和射手榜)



回复讨论(解决方案)

抓取 研究研究 phpquery

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

可以使用preg_match去抓取对应的HTML代码然后再正则过滤你想要的数据即可。

给你推荐个类 simple_html_dom

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


结果

$str=file_get_contents("http://sports.sohu.com/zhongchao.shtml");preg_match_all('/<tr>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(\d+)<\/td>\s*<td>(.+?)<\/td>\s*<\/tr>/i',$str,$match1);foreach($match1 as $k=>$v){	if($k!=0){		foreach($v as $k1=>$v1){			if($k1<=15){				$jifen[$k][]=$v1;			}else{				$sheshou[$k][]=$v1;			}		}	}}echo "<pre class="brush:php;toolbar:false">";print_r($jifen);print_r($sheshou);echo "
Copy after login
";/*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] => 上海申鑫 [13] => 河南建业 [14] => 辽宁宏运 [15] => 哈尔滨毅腾 ) [3] => Array ( [0] => 20 [1] => 19 [2] => 19 [3] => 19 [4] => 19 [5] => 19 [6] => 19 [7] => 18 [8] => 20 [9] => 19 [10] => 19 [11] => 19 [12] => 19 [13] => 19 [14] => 19 [15] => 18 ) [4] => Array ( [0] => 45 [1] => 41 [2] => 34 [3] => 31 [4] => 30 [5] => 28 [6] => 27 [7] => 25 [8] => 23 [9] => 21 [10] => 21 [11] => 20 [12] => 19 [13] => 17 [14] => 16 [15] => 12 ))Array( [1] => Array ( [0] => 01 [1] => 02 [2] => 03 [3] => 04 [4] => 04 [5] => 04 [6] => 04 [7] => 08 [8] => 09 [9] => 09 [10] => 09 [11] => 09 [12] => 09 [13] => 09 [14] => 15 [15] => 15 ) [2] => Array ( [0] => 埃尔克森 [1] => 哈默德 [2] => 海森 [3] => 达维 [4] => 多利 [5] => 洛维 [6] => 拉蒙 [7] => 德扬 [8] => 巴塔拉 [9] => 布鲁诺 [10] => 里卡多 [11] => 武磊 [12] => 埃尼奥 [13] => 尤里 [14] => 莫雷诺 [15] => 雷内 ) [3] => Array ( [0] => 17 [1] => 16 [2] => 13 [3] => 9 [4] => 9 [5] => 9 [6] => 9 [7] => 8 [8] => 7 [9] => 7 [10] => 7 [11] => 7 [12] => 7 [13] => 7 [14] => 6 [15] => 6 ) [4] => Array ( [0] => 广州恒大 [1] => 广州富力 [2] => 上海东亚 [3] => 广州富力 [4] => 哈尔滨毅腾 [5] => 山东鲁能 [6] => 杭州绿城 [7] => 北京国安 [8] => 北京国安 [9] => 大连阿尔滨 [10] => 哈尔滨毅腾 [11] => 上海东亚 [12] => 长春亚泰 [13] => 贵州茅台 [14] => 上海绿地 [15] => 广州恒大 ))*/
后面的自己处理吧

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


我输出出来的怎么是一个空数组

sohu的页面是gb2312的,采集后需要转utf8,否则会乱码

echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';$url = 'http://sports.sohu.com/zhongchao.shtml';$s = file_get_contents($url);$s = iconv('GBK','UTF8', $s); // gb2312转utf8preg_match_all('/(?<=<div class="turn cons">)\s<table.+table>/isU', $s, $m);// 获取积分榜preg_match_all('/<tr>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(\d+)<\/td>\s*<td>(.+?)<\/td>\s*<\/tr>/i',$m[0][2],$scores);$scoreboard = array();for($i=0,$len=count($scores[1]); $i<$len; $i++){	$tmp = array($scores[1][$i],strip_tags($scores[2][$i]),$scores[3][$i],$scores[4][$i]);	array_push($scoreboard, $tmp);}print_r($scoreboard);// 射手榜preg_match_all('/<tr>\s*<td>(.+?)<\/td>\s*<td>(.+?)<\/td>\s*<td>(\d+)<\/td>\s*<td>(.+?)<\/td>\s*<\/tr>/i',$m[0][3],$shooters);$shooterboard = array();for($i=0,$len=count($shooters[1]); $i<$len; $i++){	$tmp = array($shooters[1][$i],strip_tags($shooters[2][$i]),$shooters[3][$i],$shooters[4][$i]);	array_push($shooterboard, $tmp);}print_r($shooterboard);
Copy after login


积分榜
Array(    [0] => Array        (            [0] => 01            [1] => 广州恒大            [2] => 20            [3] => 45        )    [1] => Array        (            [0] => 02            [1] => 北京国安            [2] => 19            [3] => 41        )    [2] => Array        (            [0] => 03            [1] => 广州富力            [2] => 19            [3] => 34        )    [3] => Array        (            [0] => 04            [1] => 上海东亚            [2] => 19            [3] => 31        )    [4] => Array        (            [0] => 05            [1] => 贵州茅台            [2] => 19            [3] => 30        )    [5] => Array        (            [0] => 06            [1] => 山东鲁能            [2] => 19            [3] => 28        )    [6] => Array        (            [0] => 07            [1] => 天津泰达            [2] => 19            [3] => 27        )    [7] => Array        (            [0] => 08            [1] => 江苏舜天            [2] => 18            [3] => 25        )    [8] => Array        (            [0] => 09            [1] => 上海绿地            [2] => 20            [3] => 23        )    [9] => Array        (            [0] => 10            [1] => 长春亚泰            [2] => 19            [3] => 21        )    [10] => Array        (            [0] => 11            [1] => 杭州绿城            [2] => 19            [3] => 21        )    [11] => Array        (            [0] => 12            [1] => 大连阿尔滨            [2] => 19            [3] => 20        )    [12] => Array        (            [0] => 13            [1] => 上海申鑫            [2] => 19            [3] => 19        )    [13] => Array        (            [0] => 14            [1] => 河南建业            [2] => 19            [3] => 17        )    [14] => Array        (            [0] => 15            [1] => 辽宁宏运            [2] => 19            [3] => 16        )    [15] => Array        (            [0] => 16            [1] => 哈尔滨毅腾            [2] => 18            [3] => 12        ))
Copy after login


射手榜
Array(    [0] => Array        (            [0] => 01            [1] => 埃尔克森            [2] => 17            [3] => 广州恒大        )    [1] => Array        (            [0] => 02            [1] => 哈默德            [2] => 16            [3] => 广州富力        )    [2] => Array        (            [0] => 03            [1] => 海森            [2] => 13            [3] => 上海东亚        )    [3] => Array        (            [0] => 04            [1] => 达维            [2] => 9            [3] => 广州富力        )    [4] => Array        (            [0] => 04            [1] => 多利            [2] => 9            [3] => 哈尔滨毅腾        )    [5] => Array        (            [0] => 04            [1] => 洛维            [2] => 9            [3] => 山东鲁能        )    [6] => Array        (            [0] => 04            [1] => 拉蒙            [2] => 9            [3] => 杭州绿城        )    [7] => Array        (            [0] => 08            [1] => 德扬            [2] => 8            [3] => 北京国安        )    [8] => Array        (            [0] => 09            [1] => 巴塔拉            [2] => 7            [3] => 北京国安        )    [9] => Array        (            [0] => 09            [1] => 布鲁诺            [2] => 7            [3] => 大连阿尔滨        )    [10] => Array        (            [0] => 09            [1] => 里卡多            [2] => 7            [3] => 哈尔滨毅腾        )    [11] => Array        (            [0] => 09            [1] => 武磊            [2] => 7            [3] => 上海东亚        )    [12] => Array        (            [0] => 09            [1] => 埃尼奥            [2] => 7            [3] => 长春亚泰        )    [13] => Array        (            [0] => 09            [1] => 尤里            [2] => 7            [3] => 贵州茅台        )    [14] => Array        (            [0] => 15            [1] => 莫雷诺            [2] => 6            [3] => 上海绿地        )    [15] => Array        (            [0] => 15            [1] => 雷内            [2] => 6            [3] => 广州恒大        ))
Copy after login

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles