‘壹’ 用php抓取一个页面,但是这个页面需要登录才能显示,怎么抓取呢需要代码
可以使用Snoopy.class.php这个类模拟登陆,然后再抓取你想要抓取的那个页面。
‘贰’ 请问PHP模拟登录后怎么保持session总是不能成功保持session到目标站的其他页面
请问楼主 最后怎么解决的此问题 ,求公布答案, 我也是碰到这个问题了,能模拟登录成功并且获取到cookie保存,然后总是返回登录成功后 第一个看到的页面,再获取其他页面就不行了,目标服务器也是和你的问题一样,通过session 来判断
‘叁’ php curl 无法实现模拟登陆
<?php
$discuz_url = 'http://127.0.0.1/discuz/';//论坛地址
$login_url = $discuz_url .'logging.php?action=login';//登录页地址
$post_fields = array();
//以下两项不需要修改
$post_fields['loginfield'] = 'username';
$post_fields['loginsubmit'] = 'true';
//用户名和密码,必须填写
$post_fields['username'] = 'tianxin';
$post_fields['password'] = '111111';
//安全提问
$post_fields['questionid'] = 0;
$post_fields['answer'] = '';
//@todo验证码
$post_fields['seccodeverify'] = '';
//获取表单FORMHASH
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die('Not found the forumhash.');
}
//POST数据,获取COOKIE,cookie文件放在网站的temp目录下
$cookie_file = tempnam('./temp','cookie');
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);
//取到了关键的cookie文件就可以带着cookie文件去模拟发帖,fid为论坛的栏目ID
$send_url = $discuz_url."post.php?action=newthread&fid=2";
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);
//这里的hash码和登陆窗口的hash码的正则不太一样,这里的hidden多了一个id属性
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*id="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);
if(!empty($matches)) {
$formhash = $matches[1];
} else {
die('Not found the forumhash.');
}
$post_data = array();
//帖子标题
$post_data['subject'] = 'test2';
//帖子内容
$post_data['message'] = 'test2';
$post_data['topicsubmit'] = "yes";
$post_data['extra'] = '';
//帖子标签
$post_data['tags'] = 'test';
//帖子的hash码,这个非常关键!假如缺少这个hash码,discuz会警告你来路的页面不正确
$post_data['formhash']=$formhash;
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_REFERER, $send_url); //伪装REFERER
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$contents = curl_exec($ch);
curl_close($ch);
//清理cookie文件
unlink($cookie_file);
?>
‘肆’ 关于php中的snoopy问题。高手请进
我运行了一下,你这程序是可以运行的。
snoopy类不是phpstudy带的,更不是php自身就有的,它是第三方编写的一个类文件。你在网上下载一个snoopy.class.php,放在你这个程序的目录下就可以了。
‘伍’ 请教:有办法用php自动登陆189邮箱吗
可以。
你可以使用snoopy函数,实现远程登录。如果有附加码,则将网站的附加码获取到登录页面,输入,再传输。
关于史努比函数的下载,和具体用法。你在网络中输入 snoopy.class.php搜索,会找到很多文章的。
‘陆’ PHP模拟登陆QQ空间
PHP代码:
<php
$qq = "100000"; //qq号码
$pwd = "123456"; //密码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://211.139.167.71/waptest/TWF/qqportal/rela/updateuserinfo.jsp");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //这行是设定curl是否跟随header发送的location, 重要
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, "Connection", "Keep-Alive");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "qq=".$qq."&pwd=".$pwd."&FromWhere=register");
$return = curl_exec($ch);
curl_close($ch);
echo strstr($return, "postfield") ? "登录失败" : "登录成功";
?>
注:1、需curl模块支持,2、登陆后,你的好友将会看到你在线 3、不知道如何下线。
其他语言的实现类似,有兴趣的朋友可以自己动手开发一下。
这个你试试
‘柒’ php snoopy类,connection failed,
connection failed应该就是你数据库每连上····检查一下用户名和密码是否是linux下mysql的用户名和密码
‘捌’ php Snoopy模拟不成功.CURL模拟成功.这是为什么
您好,希望以下回答能帮助您
请使用SNOOPY,你网络一下就有下载地址了他是对CURL的封装,大网站很多都用这个
如您还有疑问可继续追问。
‘玖’ php curl登陆和获取内容类,请各位高手看怎么使用
由于每个网站的信息不一样,没有通用的curl的。等看你的具体需求,若你要模拟登录可以看下snoopy,一个基于curl的类,抓取信息可以用querylist