php curl抓取网页指定内容(PHP模拟登录并获取数据_post()使用的总结)
优采云 发布时间: 2021-10-29 12:05php curl抓取网页指定内容(PHP模拟登录并获取数据_post()使用的总结)
PHP模拟登录并获取数据
CURL 是一个强大的 PHP 库。使用PHP的cURL库可以轻松有效地抓取网页和采集内容,设置cookies来完成模拟登录网页,curl提供了丰富的功能,开发者可以从PHP手册中获取更多关于cURL的信息。本文以模拟登录开源中国(oschina)为例,与大家分享cURL的使用。
PHP的curl()在抓取网页的效率上比较高,并且支持多线程,而file_get_contents()的效率稍低。当然,使用curl时需要开启curl扩展。
代码实战
我们先来看看登录部分的代码:
//模拟登录 <br style="margin:0px;padding:0px;">function login_post($url, $cookie, $post) { <br style="margin:0px;padding:0px;">
$curl = curl_init();//初始化curl模块 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交 <br style="margin:0px;padding:0px;">
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息 <br style="margin:0px;padding:0px;">
curl_exec($curl);//执行cURL <br style="margin:0px;padding:0px;">
curl_close($curl);//关闭cURL资源,并且释放系统资源 <br style="margin:0px;padding:0px;">
} <br style="margin:0px;padding:0px;">
login_post()函数首先初始化curl_init(),然后使用curl_setopt()设置相关选项信息,包括要提交的URL地址、保存的cookie文件、post数据(用户名密码等信息)、返回信息等,然后curl_exec执行curl,最后curl_close()释放资源。请注意,PHP 自带的 http_build_query() 可以将数组转换为串联字符串。
接下来,如果登录成功,我们需要获取登录成功后的页面信息。
//登录成功后获取数据 <br style="margin:0px;padding:0px;">function get_content($url, $cookie) { <br style="margin:0px;padding:0px;">
$ch = curl_init(); <br style="margin:0px;padding:0px;">
curl_setopt($ch, CURLOPT_URL, $url); <br style="margin:0px;padding:0px;">
curl_setopt($ch, CURLOPT_HEADER, 0); <br style="margin:0px;padding:0px;">
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <br style="margin:0px;padding:0px;">
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie <br style="margin:0px;padding:0px;">
$rs = curl_exec($ch); //执行cURL抓取页面内容 <br style="margin:0px;padding:0px;">
curl_close($ch); <br style="margin:0px;padding:0px;">
return $rs; <br style="margin:0px;padding:0px;">
} <br style="margin:0px;padding:0px;">
函数get_content()也是先初始化curl,然后设置相关选项,执行curl,释放资源。其中,我们设置CURLOPT_RETURNTRANSFER为1自动返回信息,CURLOPT_COOKIEFILE可以读取登录时保存的cookie信息,最终返回页面内容。
我们的最终目标是获取模拟登录后的信息,即只有正常登录成功才能获取的有用信息。接下来我们以开源中国移动版为例,看看登录成功后如何抓取信息。
//设置post的数据 <br style="margin:0px;padding:0px;">$post = array ( <br style="margin:0px;padding:0px;">
'email' => 'oschina账户', <br style="margin:0px;padding:0px;">
'pwd' => 'oschina密码', <br style="margin:0px;padding:0px;">
'goto_page' => '/my', <br style="margin:0px;padding:0px;">
'error_page' => '/login', <br style="margin:0px;padding:0px;">
'save_login' => '1', <br style="margin:0px;padding:0px;">
'submit' => '现在登录' <br style="margin:0px;padding:0px;">
); <br style="margin:0px;padding:0px;">
<br style="margin:0px;padding:0px;">//登录地址 <br style="margin:0px;padding:0px;">$url = "http://m.oschina.net/action/user/login"; <br style="margin:0px;padding:0px;">//设置cookie保存路径 <br style="margin:0px;padding:0px;">$cookie = dirname(__FILE__) . '/cookie_oschina.txt'; <br style="margin:0px;padding:0px;">//登录后要获取信息的地址 <br style="margin:0px;padding:0px;">$url2 = "http://m.oschina.net/my"; <br style="margin:0px;padding:0px;">//模拟登录 <br style="margin:0px;padding:0px;">
login_post($url, $cookie, $post); <br style="margin:0px;padding:0px;">//获取登录页的信息 <br style="margin:0px;padding:0px;">$content = get_content($url2, $cookie); <br style="margin:0px;padding:0px;">//删除cookie文件 <br style="margin:0px;padding:0px;">
@ unlink($cookie); <br style="margin:0px;padding:0px;">//匹配页面信息 <br style="margin:0px;padding:0px;">$preg = "/(.*)/i"; <br style="margin:0px;padding:0px;">
preg_match_all($preg, $content, $arr); <br style="margin:0px;padding:0px;">$str = $arr[1][0]; <br style="margin:0px;padding:0px;">//输出内容 <br style="margin:0px;padding:0px;">echo $str; <br style="margin:0px;padding:0px;">
运行上面的代码后,我们会看到最终得到了登录用户的头像。
使用总结
1、初始化curl;
2、使用curl_setopt设置目标url,以及其他选项;
3、curl_exec,执行curl;
4、 执行后关闭curl;
5、 输出数据。
参考
《php中curl和curl介绍》,作者不详,
“使用 PHP CURL 发布数据”,作者:Veda,
《php使用curl模拟登录discuz,模拟发帖》,作者:tianxin,
免责声明:本文为原创文章,版权归作者所有。如需转载,请注明出处并保留原文链接: