很多站长朋友们都不太清楚phpcurl如何采集,今天小编就来给大家整理phpcurl如何采集,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php中curl爬虫 怎么样通过网页获取所有链接 2、 如何用php CURL 抓取微信网页的内容 3、 php curl采集搜索结果要怎么写 4、 php的curl怎么爬取网页内容 5、 php curl 大量数据采集 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 CURL 抓取微信网页的内容给你简单介绍几个吧
一、file_get_contents函数
$content = file_get_contents("URL");//URL就是你要获取的页面的地址
二、利用curl扩展
代码如下:
function getCurl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
PS:需要安装PHP的curl扩展
php curl采集搜索结果要怎么写首先用curl获取 www。baidu。com/s?wd=你的关键词 的网页内容,然后使用正则过滤出第一页的搜索结果。
然后 在用 curl 获取 www。baidu。com/s?wd=你的关键词pn=10 第二页的数据,同样用正则解析出搜索结果,继续获取 pn=20 pn=30 pn=40等依次类推。
php的curl怎么爬取网页内容创建一个新cURL资源
设置URL和相应的选项
抓取URL并把它传递给浏览器
关闭cURL资源,并且释放系统资源
代码案例:
php curl 大量数据采集这个需要配合js,打开一个html页面,首先js用ajax请求页面,返回第一个页面信息确定处理完毕(ajax有强制同步功能),ajax再访问第二个页面。(或者根据服务器状况,你可以同时提交几个URL,跑几个相同的页面)
参数可以由js产生并传递url,php后台页面根据URL抓页面。然后ajax通过php,在数据库或者是哪里设一个标量,标明检测到哪里。由于前台的html页面执行多少时候都没问题,这样php的内存限制和执行时间限制就解决了。
因为不会浪费大量的资源用一个页面来跑一个瞬间500次的for循环了。(你的500次for循环死了原因可能是获取的数据太多,大过了php限制的内存)
不过印象中curl好像也有强制同步的选项,就是等待一个抓取后再执行下一步。但是这个500次都是用一个页面线程处理,也就是说肯定会远远大于30秒的默认执行时间。
关于phpcurl如何采集的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpcurl如何采集 php 采集的详细内容...