php文章内容抓取
求大神帮忙抓取这个网页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]));
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>......
可以使用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;
结果
$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 "
后面的自己处理吧
$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]));
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>......
我输出出来的怎么是一个空数组
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);
积分榜
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 ))
射手榜
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] => 广州恒大 ))

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
