资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用php获取网页数据 用php获取网页数据的方法

php怎么抓取其它网站数据

可以用以下4个方法来抓取网站 的数据:

创新互联于2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元紫金做网站,已为上家服务,为紫金各地企业和个人服务,联系电话:13518219792

1. 用 file_get_contents 以 get 方式获取内容:

?

$url = '';

$html = file_get_contents($url);

echo $html;

2. 用fopen打开url,以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 = array(

'foo'='bar',

'baz'='boom',

'site'='',

'name'='nowa magic');

$data = http_build_query($data);

//$postdata = 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获取网页部分数据

如果你要

之间的所有源码,用 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获取网页内容的几种方法

简单的收集下PHP下获取网页内容的几种方法:

用file_get_contents,以get方式获取内容。

用fopen打开url,以get方式获取内容。

使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展。

用file_get_contents函数,以post方式获取url。

用fopen打开url,以post方式获取内容。

用fsockopen函数打开url,获取完整的数据,包括header和body。

PHP 如何获取到一个网页的内容

1.file_get_contents

PHP代码

复制代码 代码如下:

?php

$url = "";

$contents = file_get_contents($url);

//如果出现中文乱码使用下面代码

//$getcontent = iconv("gb2312", "utf-8",$contents);

echo $contents;

?

2.curl

PHP代码

复制代码 代码如下:

?php

$url = "";

$ch = curl_init();

$timeout = 5;

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//在需要用户检测的网页里需要增加下面两行

//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);

$contents = curl_exec($ch);

curl_close($ch);

echo $contents;

?

3.fopen-fread-fclose

PHP代码

复制代码 代码如下:

?php

$handle = fopen ("", "rb");

$contents = "";

do {

$data = fread($handle, 1024);

if (strlen($data) == 0) {

break;

}

$contents .= $data;

} while(true);

fclose ($handle);

echo $contents;

?

注:

1.

使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置

allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。

2.使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分

号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩

展。

php获取指定网页内容

一、用file_get_contents函数,以post方式获取url

?php

$url= '';

$data= array('foo'= 'bar');

$data= http_build_query($data);

$opts= array(

'http'= array(

'method'= 'POST',

'header'="Content-type: application/x-www-form-urlencoded\r\n"  .

"Content-Length: "  . strlen($data) . "\r\n",

'content'= $data

)

);

$ctx= stream_context_create($opts);

$html= @file_get_contents($url,'',$ctx);

二、用file_get_contents以get方式获取内容

?php

$url='';

$html= file_get_contents($url);

echo$html;

?

三、用fopen打开url, 以get方式获取内容

?php

$fp= fopen($url,'r');

$header= stream_get_meta_data($fp);//获取报头信息

while(!feof($fp)) {

$result.= fgets($fp, 1024);

}

echo"url header: {$header} br":

echo"url body: $result";

fclose($fp);

?

四、用fopen打开url, 以post方式获取内容

?php

$data= array('foo2'= 'bar2','foo3'='bar3');

$data= http_build_query($data);

$opts= array(

'http'= array(

'method'= 'POST',

'header'="Content-type: application/x-www-form-

urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n"  .

"Content-Length: "  . strlen($data) . "\r\n",

'content'= $data

)

);

$context= stream_context_create($opts);

$html= fopen(';id2=i4','rb',false, $context);

$w=fread($html,1024);

echo$w;

?

五、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

?php

$ch= curl_init();

$timeout= 5;

curl_setopt ($ch, CURLOPT_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采集网站数据

简单的分了几个步骤:

1、确定采集目标

2、获取目标远程页面内容(curl、file_get_contents)

3、分析页面html源码,正则匹配你需要的内容(preg_match、preg_match_all),这一步最为重要,不同页面正则匹配规则不一样

4、入库


新闻标题:用php获取网页数据 用php获取网页数据的方法
标题路径:http://cdkjz.cn/article/doseopd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220