很多站长朋友们都不太清楚php网页爬虫,今天小编就来给大家整理php网页爬虫,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 如何用php 编写网络爬虫 2、 php 实现网络爬虫 3、 如何用php 编写网络爬虫? 4、 如何用PHP做网络爬虫 5、 php中curl爬虫 怎么样通过网页获取所有链接 如何用php 编写网络爬虫php不太适合用来写网络爬虫,因为几乎没有现成的框架,或者成熟的下载机制,也不太适合做并发处理.
下载页面的话除了一个curl,就是file_get_contents,或者curl_multi来做并发请求.curl可以代理端口,虚假ip,带cookie,带header请求目标页面,下载完成之后解析页面可以用queryList来解析html.写法类似jQuery.
提供给你我之前写的类:curl.php ?希望可以帮到你.
QueryList.php和phpQuery.php由于文件太大了,没办法贴上来
<?php
class?Http?{
????public?function?curlRequest($url,?$postData?=?'',?$timeOut?=?10,?$httpHeader?=?array())?{
????????$handle?=?curl_init?();
????????curl_setopt?(?$handle,?CURLOPT_URL,?$url?);
????????if?($httpHeader)?{
????????????curl_setopt($handle,?CURLOPT_HTTPHEADER,?$httpHeader);
????????}
????????curl_setopt?(?$handle,?CURLOPT_RETURNTRANSFER,?true?);
????????curl_setopt?(?$handle,?CURLOPT_HEADER,?0?);????????????????????????????????????????????????????????????????curl_setopt?(?$handle,?CURLOPT_TIMEOUT,?$timeOut?);
????????curl_setopt?(?$handle,?CURLOPT_FOLLOWLOCATION,?1?);
????????curl_setopt?(?$handle,?CURLOPT_SSL_VERIFYPEER,?false?);
????????curl_setopt?(?$handle,?CURLOPT_SSL_VERIFYHOST,?false?);
????????curl_setopt?(?$handle,?CURLOPT_USERAGENT,?'Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10_7_2)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/27.0.1453.93?Safari/537.36');????????curl_setopt?(?$handle,?CURLOPT_ENCODING,?'gzip,deflate,sdch');
????????if?(!?empty?(?$postData?))?{
????????????curl_setopt?(?$handle,?CURLOPT_POST,?1?);
????????????curl_setopt?(?$handle,?CURLOPT_POSTFIELDS,?$postData);
????????}
????????$result['response']?=?curl_exec?(?$handle?);
????????$result['httpStatus']?=?curl_getinfo?(?$handle,?CURLINFO_HTTP_CODE?);
????????$result['fullInfo']?=?curl_getinfo?(?$handle?);
????????$result['errorMsg']?=?'';
????????$result['errorNo']?=?0;
????????if?(curl_errno($handle))?{
????????????$result['errorMsg']?=?curl_error($handle);
????????????$result['errorNo']?=?curl_errno($handle);
????????}
????????curl_close?(?$handle?);
????????return?$result;
????}
}
?>
php 实现网络爬虫pcntl_fork或者swoole_process实现多进程并发。按照每个网页抓取耗时500ms,开200个进程,可以实现每秒400个页面的抓取。
curl实现页面抓取,设置cookie可以实现模拟登录
simple_html_dom 实现页面的解析和DOM处理
如果想要模拟浏览器,可以使用casperJS。用swoole扩展封装一个服务接口给PHP层调用
在这里有一套爬虫系统就是基于上述技术方案实现的,每天会抓取几千万个页面。
如何用php 编写网络爬虫?pcntl_fork或者swoole_process实现多进程并发。按照每个网页抓取耗时500ms,开200个进程,可以实现每秒400个页面的抓取。
curl实现页面抓取,设置cookie可以实现模拟登录
simple_html_dom 实现页面的解析和DOM处理
如果想要模拟浏览器,可以使用casperJS。用swoole扩展封装一个服务接口给PHP层调用
在这里有一套爬虫系统就是基于上述技术方案实现的,每天会抓取几千万个页面。
如何用PHP做网络爬虫其实用PHP来爬会非常方便,主要是PHP的正则表达式功能在搜集页面连接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函数非常方便的下载网页内容。
具体处理方式就是建立就一个任务队列,往队列里面插入一些种子任务和可以开始爬行,爬行的过程就是循环的从队列里面提取一个URL,打开后获取连接插入队列中,进行相关的保存。队列可以使用数组实现。
当然PHP作为但线程的东西,慢慢爬还是可以,怕的就是有的URL打不开,会死在那里。
php中curl爬虫 怎么样通过网页获取所有链接本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集。一般php采集网络数据会用file_get_contents、file和cURL。不过据说cURL会比file_get_contents、file更快更专业,更适合采集。今天就试试用cURL来获取网页上的所有链接。示例如下:
<?php
/*
* 使用curl 采集hao123测试数据下的所有链接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主机部分,补全用
$host = '';
if (is_array($linkarr)) {
foreach ($linkarr as $k => $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("<p>此页面的所有链接为:</p><pre>%s</pre>n", var_export($linkresult , true));
?>
function.php内容如下(即为上两篇中两个函数的合集):
<?php
function _striplinks($document) {
preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?>
关于php网页爬虫的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。