使用python/login/ target=_blank class=infotextkey>python模拟登录网站,首先要知道网站登录细节及相应的cookie。
分三步完成:
1,获取一个cookie
2,装载好自己的request
3,发送模拟登录请求
推荐阅读:
例子,python模拟登录网站的代码。
#author:liangliang
#email:liangliangyy@gmail.com
#blog:http://www.lylinux.org/
#site: http://www.jb200.com
import urllib
import urllib2
import cookielib
import string
import re
url = "http://www.lylinux.org/wp-login.php"
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',}
#这块可以通过抓包工具来获得
postData = {
'log':'admin',
'pwd':'password',
'wp-submit':'登录',
'redirect_to':'http://www.lylinux.org/wp-admin/',
'testcookie':'1'
}
cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)
h = urllib2.urlopen(url)
postData = urllib.urlencode(postData)
request = urllib2.Request(url,postData,headers)
print request
response = urllib2.urlopen(request)
text = response.read()
text = unicode(text,'utf-8').encode('gb18030')
print text