php curl抓取网页指定内容(使用PHP的cURL库可以简单和有效地去抓网页。)
优采云 发布时间: 2022-01-05 09:11php curl抓取网页指定内容(使用PHP的cURL库可以简单和有效地去抓网页。)
使用 PHP 的 cURL 库来简单有效地抓取网页。你只需要运行一个脚本,然后分析你抓取的网页,然后你就可以通过编程的方式得到你想要的数据。无论您是想从链接中获取部分数据,还是获取 XML 文件并将其导入数据库,即使只是获取网页内容,cURL 都是一个强大的 PHP 库。本文主要介绍如何使用这个PHP库。
启用卷曲设置
首先我们要先判断我们的PHP是否启用了这个库,可以通过php_info()函数来获取这个信息。
﹤?php<br style="clear:both; width:0px; height:0px" />phpinfo();<br style="clear:both; width:0px; height:0px" />?﹥
如果在网页上可以看到如下输出,说明cURL库已经开启。
如果你看到它,那么你需要设置你的 PHP 并启用这个库。如果你是windows平台,很简单,你需要改变你的php.ini文件的设置,找到php_curl.dll,去掉前面的分号。如下:
//取消下在的注释<br style="clear:both; width:0px; height:0px" />extension=php_curl.dll
如果你在 Linux 下,那么你需要重新编译你的 PHP。编辑时需要开启编译参数——在configure命令中添加“--with-curl”参数。
一个小例子
如果一切就绪,这里有一个小程序:
﹤?php
// 初始化一个 cURL 对象
$curl = curl_init();
// 设置你需要爬取的网址
curl_setopt($curl, CURLOPT_URL,'#39;);
// 设置标题
curl_setopt($curl, CURLOPT_HEADER, 1);
// 设置cURL参数,询问结果是保存在字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行 cURL 并请求一个网页
$data = curl_exec($curl);
// 关闭 URL 请求
curl_close($curl);
// 显示获取的数据
var_dump($data);
如何发布数据
上面是获取网页的代码,下面是到某个网页的POST数据。假设我们有一个处理表单的URL,它可以接受两个表单域,一个是电话号码,一个是短信的内容。
﹤?php<br style="clear:both; width:0px; height:0px" />$phoneNumber = '13912345678';<br style="clear:both; width:0px; height:0px" />$message = 'This message was generated by curl and php';<br style="clear:both; width:0px; height:0px" />$curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';<br style="clear:both; width:0px; height:0px" />$ch = curl_init();<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/sendSMS.php');<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_HEADER, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_POST, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);<br style="clear:both; width:0px; height:0px" />$data = curl_exec();<br style="clear:both; width:0px; height:0px" />curl_close($ch);<br style="clear:both; width:0px; height:0px" />?﹥
从上面的程序可以看出,CURLOPT_POST是用来设置HTTP协议的POST方法而不是GET方法的,然后CURLOPT_POSTFIELDS是用来设置POST数据的。
关于代理服务器
以下是如何使用代理服务器的示例。请注意高亮的代码,代码很简单,我就不多说了。
﹤?php <br style="clear:both; width:0px; height:0px" />$ch = curl_init();<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_URL, 'http://www.example.com');<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_HEADER, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_PROXY, 'fakeproxy.com:1080');<br style="clear:both; width:0px; height:0px" />curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');<br style="clear:both; width:0px; height:0px" />$data = curl_exec();<br style="clear:both; width:0px; height:0px" />curl_close($ch);<br style="clear:both; width:0px; height:0px" />?﹥
关于 SSL 和 Cookie