Home > php教程 > php手册 > PHP实现简单爬虫的方法

PHP实现简单爬虫的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:50:14
Original
892 people have browsed it

这篇文章主要介绍了PHP实现简单爬虫的方法,涉及php页面抓取及分析的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP实现简单爬虫的方法。分享给大家供大家参考。具体如下:

\'\"\ ]*).*?>/'; $result = preg_match_all($reg_tag_a, $web_content, $match_result); if ($result) { return $match_result[1]; } } /** * 修正相对路径 * * @param string $base_url * @param array $url_list * @return array */ function _reviseUrl($base_url, $url_list) { $url_info = parse_url($base_url); $base_url = $url_info["scheme"] . '://'; if ($url_info["user"] && $url_info["pass"]) { $base_url .= $url_info["user"] . ":" . $url_info["pass"] . "@"; } $base_url .= $url_info["host"]; if ($url_info["port"]) { $base_url .= ":" . $url_info["port"]; } $base_url .= $url_info["path"]; print_r($base_url); if (is_array($url_list)) { foreach ($url_list as $url_item) { if (preg_match('/^http/', $url_item)) { // 已经是完整的url $result[] = $url_item; } else { // 不完整的url $real_url = $base_url . 'http://www.jb51.net/' . $url_item; $result[] = $real_url; } } return $result; } else { return; } } /** * 爬虫 * * @param string $url * @return array */ function crawler($url) { $content = _getUrlContent($url); if ($content) { $url_list = _reviseUrl($url, _filterUrl($content)); if ($url_list) { return $url_list; } else { return ; } } else { return ; } } /** * 测试用主程序 */ function main() { $current_url = "http://hao123.com/"; //初始url $fp_puts = fopen("url.txt", "ab"); //记录url列表 $fp_gets = fopen("url.txt", "r"); //保存url列表 do { $result_url_arr = crawler($current_url); if ($result_url_arr) { foreach ($result_url_arr as $url) { fputs($fp_puts, $url . "\r\n"); } } } while ($current_url = fgets($fp_gets, 1024)); //不断获得url } main(); ?>

希望本文所述对大家的php程序设计有所帮助。

Related labels:
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
Latest Issues
ubuntu mongodb 出现 exception: connect failed
From 1970-01-01 08:00:00
0
0
0
An error occurred
From 1970-01-01 08:00:00
0
0
0
Why does clicking login jump to hello world?
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template