php版:
<?php
$cookie_file = fopen('cookie.txt','w');//dirname(__FILE__)."/cookie_".md5(basename(__FILE__)).".txt"; // 設定Cookie檔案儲存路徑及檔案名
function vlogin($url,$data){ // 模拟登入擷取Cookie函數
$curl = curl_init(); // 啟動一個CURL會話
curl_setopt($curl, CURLOPT_URL, $url); // 要通路的位址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟使用者使用的浏覽器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設定Referer
curl_setopt($curl, CURLOPT_POST, 1); // 發送一個正常的Post請求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post送出的資料包
curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie資訊的檔案名稱
curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 讀取上面所儲存的Cookie資訊
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設定逾時限制防止死循環
curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示傳回的Header區域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 擷取的資訊以檔案流的形式傳回
$tmpInfo = curl_exec($curl); // 執行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 關閉CURL會話
return $tmpInfo; // 傳回資料
}
function vget($url){ // 模拟擷取内容函數
$curl = curl_init(); // 啟動一個CURL會話
curl_setopt($curl, CURLOPT_URL, $url); // 要通路的位址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟使用者使用的浏覽器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設定Referer
curl_setopt($curl, CURLOPT_HTTPGET, 1); // 發送一個正常的Post請求
curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 讀取上面所儲存的Cookie資訊
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設定逾時限制防止死循環
curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示傳回的Header區域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 擷取的資訊以檔案流的形式傳回
$tmpInfo = curl_exec($curl); // 執行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 關閉CURL會話
return $tmpInfo; // 傳回資料
}
function vpost($url,$data){ // 模拟送出資料函數
$curl = curl_init(); // 啟動一個CURL會話
curl_setopt($curl, CURLOPT_URL, $url); // 要通路的位址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟使用者使用的浏覽器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設定Referer
curl_setopt($curl, CURLOPT_POST, 1); // 發送一個正常的Post請求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post送出的資料包
curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 讀取上面所儲存的Cookie資訊
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設定逾時限制防止死循環
curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示傳回的Header區域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 擷取的資訊以檔案流的形式傳回
$tmpInfo = curl_exec($curl); // 執行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl); // 關鍵CURL會話
return $tmpInfo; // 傳回資料
}
function delcookie($cookie_file){ // 删除Cookie函數
@unlink($cookie_file); // 執行删除
}
// 使用執行個體
if(!file_exists($cookie_file)) { // 檢測Cookie是否存在
vlogin('http://example.com/login_do.asp','id=xxx&password=xxx'); // 登入擷取Cookie
}
echo vget('http://example.com/upload_new.asp');
delcookie($cookie_file);
?>
python版(1):使用urllib和cookielib
import urllib
import urllib2
import cookielib
def xxxFunc():
"""
"""
ck = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(ck))
opener.addheaders = [('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 528; POTU(RR:28031409:0:5491841); Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)')]
opener.addheaders = [('Accept', '*/*')]
opener.addheaders = [('Accept-Language', 'zh-cn')]
opener.addheaders = [('Accept-Encoding', 'gzip, deflate')]
opener.addheaders = [('Connection', 'Keep-Alive')]
opener.addheaders = [('Cache-Control', 'no-cache')]
urllib2.install_opener(opener)
url_login = 'http://xxx.net/login'
body = (('id','xxx'), ('pw','xxx'))
req = urllib.urlopen(url_login,urllib.urlencode(body))
req = urllib2.urlopen('http://xxx.net/post')
python版(2):使用ClientCookie
import ClientCookie
request = ClientCookie.Request("https://example.com/?login&username=xxx&password=xxx")
response = ClientCookie.urlopen(request)
request2 = ClientCookie.Request("http://example.com/xxx")
response2 = ClientCookie.urlopen(request2)
print response2.read()
上述的方法對于一般的網站都能夠模拟登陸,達到預期的目的,不過我想模拟登陸的網站很遺憾沒能成功,cookie儲存不下來。