php采集网页插件数据 php抓取网页数据

php中curl爬虫 怎么样通过网页获取所有链接本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集 。一般php采集网络数据会用file_get_contents、file和cURL 。不过据说cURL会比file_get_contents、file更快更专业,更适合采集 。今天就试试用cURL来获取网页上的所有链接 。示例如下:
?php
/*
* 使用curl 采集hao123.com下的所有链接 。
*/
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此页面的所有链接为:/ppre%s/pren", 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:$linksthe links to qualify
$URIthe 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]);
【php采集网页插件数据 php抓取网页数据】 $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获取网页部分数据如果你要

之间的所有源码,用 preg_match 就可以,不用preg_match_all ,如果你要里面的所有的
标签中的内容,可以用preg_match_all //提取所有代码 $pattern = '/
(. ?)
/is'; preg_match($pattern, $string, $match); //$match[0] 即为

之间的所有源码 echo $match[0]; //然后再提取
之间的内容 $pattern = '/(. ?)li/is'; preg_match_all($pattern, $match[0], $results); $new_arr=array_unique($results[0]); foreach($new_arr as $kkk){ echo $kkk; }
php怎么抓取其它网站数据可以用以下4个方法来抓取网站 php采集网页插件数据的数据php采集网页插件数据:
1. 用 file_get_contents 以 get 方式获取内容:
?
$url = '';
$html = file_get_contents($url);
echo $html;
2. 用fopen打开urlphp采集网页插件数据,以get方式获取内容
?
$url = '';
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
$result = '';
while(!feof($fp))
{
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
3. 用file_get_contents函数,以post方式获取url
?
$data = https://www.04ip.com/post/array(
'foo'='bar',
'baz'='boom',
'site'='',
'name'='nowa magic');
$data = https://www.04ip.com/post/http_build_query($data);
//$postdata = https://www.04ip.com/post/http_build_query($data);
$options = array(
'http' = array(
'method' = 'POST',
'header' = 'Content-type:application/x-www-form-urlencoded',
'content' = $data
//'timeout' = 60 * 60 // 超时时间(单位:s)
)
);
$url = "";
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
4、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$url = '';
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
PHP怎样抓取网页代码中动态(Ajax)显示的数据你是想抓别人网页上ajax动态载入的数据吧? 1、要找到它的ajax载入的URL地址 2、利用PHP的file_get_contents($url)函数读取那个url地址 。3、对抓取到的内容进行分析或正则过滤 。
关于php采集网页插件数据和php抓取网页数据的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读