之前用php寫的curl模擬登陸
現在改成Python寫,Python確實強大,幾行代碼搞定
簡單說一下流程:先用cookielib獲取cookie,在用獲取到的cookie,進入主頁面
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Filename : biuman.py
import urllib2
import urllib
import cookielib
import re
auth_url = 'http://www.biuman.com/auth'
home_url = 'http://www.biuman.com/home';
#登陸用戶名和密碼
data={
"username":"www.biuman.com",
"password":"biuman"
}
#urllib進行編碼
post_data=urllib.urlencode(data)
#發送頭信息
headers ={
"Host":"login.biuman.com",
"Referer": "http://www.biuman.com"
}
# 初始化一個CookieJar來處理Cookie
cookieJar=cookielib.CookieJar()
#實例化一個全局opener
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
#獲取cookie
req=urllib2.Request(auth_url,post_data,headers)
result = opener.open(req)
#訪問主頁 自動帶著cookie信息
result = opener.open(home_url)
#顯示結果
print result.read()