最近在忙一个简历导入的模块, 类似博客搬家或者邮箱搬家,但是简历导入功能需要用户登陆以后才能获取简历数据。其中用到了模拟登陆的功能。
那如何实现模拟登陆?我们知道一般的网站都是通过Cookies来维护状态的,我抓的网站也是支持利用Cookies来验证用户的,构造一个post数据包,向服务器提 交数据。本来一开始是用curl这个php扩展来进行模拟登陆的,可惜的是服务器没有编译这个扩展。所以编好了的程序就白费了~不过我在这贴一下代码吧,省的浪费~~
用curl进行模拟登陆中华英才
curl 是一个利用URL语法在命令行方式下工作的文件传输工具。
它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,
上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。
<?php
header(”Content-type: text/html;charset=utf-8″);
include(’./chinahr/chinahrSpider.php’);
include(’./array2xml/arr2xml.class.php’);
//CURLOPT_URL是要得到内容的地址
//CURLOPT_HEADER是要得到的头
//CURLOPT_NOBODY是要得到的内容
//CURLOPT_PORT是端口
//CURLOPT_RETURNTRANSFER是要把输出的内容放到buffer中,可以被echo或者赋予某变量
//CURLOPT_POST是标准的提交
//CURLOPT_POSTFIELDS是提交的内容
//CURLOPT_COOKIEJAR是要保存的cookie
//CURLOPT_COOKIEFILE是从文件读取cookie并提交
//CURLOPT_FOLLOWLOCATION启用时会将服务器服务器返回的“Location:”放在header中递归的返回给服务器
//CURLOPT_MAXREDIRS限定递归返回的数量
//CURLOPT_HTTPHEADER设置一个header中传输内容的数组
//CURLOPT_REFERER设置header中”Referer:”部分的值
//CURLOPT_USERAGENT在HTTP请求中包含一个”user-agent”头的字符串。
//CURLOPT_ENCODING设定header中“Accept-Encoding: ”部分的内容,支持的编码格式为:”identity”,”deflate”,”gzip”。如果设置为空字符串,则表示支持所有的编码格式
//CURLOPT_COOKIE设定HTTP请求中Set-Cookie:部分的内容
$url = ‘http://my.chinahr.com/login.aspx’;
$post = 1;
$returntransfer = 1;
$port = 80;
$header = 0;
$nobody = 0;
$followlocation = 1;
$cookie_jar = tempnam(’./tmp’,'cookie.txt’);
/**
* 进入登陆页 http://my.chinahr.com/login.aspx
*/
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_NOBODY => $nobody,
CURLOPT_PORT => $port,
CURLOPT_RETURNTRANSFER => $returntransfer,
CURLOPT_FOLLOWLOCATION => $followlocation,
CURLOPT_COOKIEJAR => $cookie_jar
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
/**
* 提交账号密码登陆
*/
$request = ‘LoginModule_ascx$tbUserName=’.$username.’&LoginModule_ascx$tbPassword=’.$password;
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => $header,
CURLOPT_NOBODY => $nobody,
CURLOPT_PORT => $port,
CURLOPT_POST => $post,
CURLOPT_POSTFIELDS => $request,
CURLOPT_RETURNTRANSFER => $returntransfer,
CURLOPT_FOLLOWLOCATION => $followlocation,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_COOKIEFILE => $cookie_jar,
CURLOPT_REFERER => $url
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
/**
* 登陆成功后进入到 http://my.chinahr.com/resume/index.aspx 获取简历修改页链接
*/
$ch = curl_init();
$options = array(
CURLOPT_URL => ‘http://my.chinahr.com/resume/index.aspx’,
CURLOPT_HEADER => $header,
CURLOPT_NOBODY => $nobody,
CURLOPT_PORT => $port,
CURLOPT_RETURNTRANSFER => $returntransfer,
CURLOPT_FOLLOWLOCATION => $followlocation,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_COOKIEFILE => $cookie_jar,
);
curl_setopt_array($ch, $options);
$htmlContent=curl_exec($ch); //默认输出utf-8编码的字符串
curl_close($ch);
$pattern=iconv(’GB2312′,’UTF-8′,’/<a href=”..\/Resume55\/(.*?)&lang=0″>修改<\/a>/’);
preg_match_all($pattern,$htmlContent,$resumeLinks);
unset($htmlContent);
$xml = new xml();
foreach ($resumeLinks[1] as $links){
$ch = curl_init();
$options = array(
CURLOPT_URL => ‘http://my.chinahr.com/Resume55/’.$links.’&lang=0′,
CURLOPT_HEADER => $header,
CURLOPT_NOBODY => $nobody,
CURLOPT_PORT => $port,
CURLOPT_RETURNTRANSFER => $returntransfer,
CURLOPT_FOLLOWLOCATION => $followlocation,
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_COOKIEFILE => $cookie_jar,
);
curl_setopt_array($ch, $options);
$resumeLists[]=chinahrSpider(curl_exec($ch));
//$xml->setArray(chinahrSpider(curl_exec($ch)));
//echo $str = $xml->outputXML();
curl_close($ch);
//创建xml文件.
/*if($resource = fopen(”./create.xml”,”w”)){
fwrite($resource,$str);
}
fclose ($resource);*/
}
unlink($cookie_jar);
print_r($resumeLists)
?>
用一个snoopy的http类进行模拟登陆智联
简单的说,snoopy是一个用以模拟浏览器功能的php类,它可以模拟访问,提交表单到你需要登录的web服务器端,同时提供了筛选信息的功能,让你可以根据需要取得自己想要的信息。
<?php
include(’./Snoopy.class.php’);
include(’./zhilianSpider.php’);
header(”content-type:text/html; charset=utf-8″);
$snoopy = new Snoopy;
$formvars["loginname"] = ‘test’;
$formvars["password"] = ‘test’;
$action =” http://my.zhaopin.com/loginmgr/loginproc.asp “;
$snoopy->submit($action,$formvars);
$snoopy->fetchlinks(’http://my.zhaopin.com/myzhaopin/resume_list.asp’);
$allLinks=$snoopy->results;
$matchResumeLinks=’http://my.zhaopin.com/MYZHAOPIN/editName.asp’;
foreach ($allLinks as $value) {
$findResumeLinks=substr($value,0,44);
$previewResumeLinks=substr($value,44);
if($findResumeLinks==$matchResumeLinks){
$snoopy->fetch(’http://my.zhaopin.com/myzhaopin/resume_preview.asp’.$previewResumeLinks.’&LocationUrl=resume_list’);
$htmlContent=array_pop($snoopy->results);
$resumeLists[]=zhilianSpider($htmlContent);
}
}
unset($allLinks);
print_r($resumeLists);
?>
用PEAR :: Package :: HTTP_Request来模拟登陆智联
有时候我们需要在服务器端模拟浏览器来完成一些http的请求,或者需要自动化某些http的请求,很多内容例如post,cookie,代理服务器,访问来源都比较难模拟,用Http_request这个php的pear包可以很方便的完成这些功能,相当简单。
Http_request这个代码包可以简单的实现http请求处理。它支持GET/POST/HEAD/TRACE/PUT/DELETE请求方法,基本的认证,代理,代理认证,ssl,文件上传等功能。
<?php
include(’./NetHttp/Request.php’);
//header(”content-type:text/html; charset=utf-8″);
class getzhilian{
var $username=”;
var $password=”;
function getzhilian($username,$password){
$this->username=$username; //获得用户名
$this->password=$password; //获得密码
}
function getResume(){
/**
* post登陆智联 用户名密码提交至 http://my.zhaopin.com/loginmgr/loginproc.asp
*/
$req = &new HTTP_Request(’http://my.zhaopin.com/loginmgr/login.asp’);
$req->clearCookies();
$req->setMethod(HTTP_REQUEST_METHOD_POST);
$req->setURL(” http://my.zhaopin.com/loginmgr/loginproc.asp “);
$req->addPostData(”loginname”,$this->username);
$req->addPostData(”password”,$this->password);
$req->sendRequest();
$response = $req->getResponseBody();
if(preg_match(’/Template not found/’,$response,$islogin)){
return ‘loginfailed’; //判端是否登录成功,如果没有成功可能用户名和密码输入错误
}
// 获得站点cookies 并发送
$cookies = $req->getResponseCookies();
foreach ($cookies as $cookieArr) {
$req->addCookie($cookieArr['name'],$cookieArr['value']);
}
//进入智联简历管理页面 http://my.zhaopin.com/myzhaopin/resume_list.asp
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->setURL(’http://my.zhaopin.com/myzhaopin/resume_list.asp’);
$req->sendRequest();
$response = $req->getResponseBody(); //得到简历管理页面内容
//中文[<a href="resume_preview_edit.asp?ext_id=JR230703544R90250010000&resume_id=136381273&Version_Number=1&language_id=1&LocationUrl=resume_list" title="">已完成</a>]
$resumeLinks_pattern=’/中文\[<a href="resume_preview_edit.asp\?(.*?)" title="">已完成<\/a>]/’;
$count= preg_match_all($resumeLinks_pattern,$response,$links); //匹配出中文简历ID等信息
//访问智联简历预览页面
if($count){
foreach ($links[1] as $key=>$value) {
$previewLink=’http://my.zhaopin.com/myzhaopin/resume_preview.asp?’.$value.’&Version_Number=1&language_id=1&LocationUrl=resume_list’;
preg_match(’/&resume_id=(.*?)&/’,$value,$resumeId);
$resumeId=trim($resumeId[1]); //得到简历ID
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->setURL($previewLink);
$req->sendRequest();
$response = $req->getResponseBody(); //得到预览页内容
file_put_contents(’./date_’.$key.’.html’,$response);
}
}else {
return ‘noresume’; //没有简历
}
}
}
?>
转载请注明文章出处http://www.bao00long.cn/2009/resumespider .html