ijd8.COM

A simple blog for an old Ma Nong.

关于python cookie 登录

Permalink

一些 python cookie 登录的方法记录。

不使用Cookie, 简单的POST登录

Python: urllib2 post登录
1
2
3
4
5
6
7
8
import urllib2, urllib

data = {'name' : 'test', 'password' : 'testpw'}
f = urllib2.urlopen(
        url     = 'http://www.test.com/',
        data    = urllib.urlencode(data)
		)
print f.read()

用urllib2,cookielib cookie 登录

Python: python urllib2 cookie登录
1
2
3
4
5
6
7
import urllib2,cookielib
import urllib
cookie=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
data=urllib.urlencode({'username':'test','password':'testpw'})
login_response=urllib2.urlopen('http://www.test.com', data)

获取cookie值并返回

Python: 获取cookie并保存 http://www.zhihu.com/question/20468941 zhihu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import urllib,urllib2,cookielib 

cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_path = 'http://xxxxxxxxxx.com/login'

data = {"name":"xxxxxxx","passwd":"xxxxxxxx"}
post_data = urllib.urlencode(data)
request = urllib2.Request(login_path,post_data)
html = opener.open(request).read()

if cj:
    print cj
    cj.save('cookiefile.txt')

用requests cookie登录

Python: requests cookie登录 https://gist.github.com/kennethreitz/973705 github
1
2
3
4
5
6
7
8
9
10
11
12
import requests
 
r = requests.get('https://api.github.com', auth=('user', 'pass'))
 
print r.status_code
print r.headers['content-type']

#以下也是一个例子
import requests
s = requests.session()
s.get("http://example.com")
s.get("http://example.com/some/page/you/want")

Request 文档

用python 模拟登录微博

Python: 登录微博 http://www.douban.com/note/201767245/ douban
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-

import urllib
import urllib2
import cookielib
import base64
import re
import json
import hashlib

#获取一个保存cookie的对象
cj = cookielib.LWPCookieJar()
#将一个保存cookie对象,和一个HTTP的cookie的处理器绑定
cookie_support = urllib2.HTTPCookieProcessor(cj)
#创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的URL的打开
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
#将包含了cookie、http处理器、http的handler的资源和urllib2对象板顶在一起
urllib2.install_opener(opener)

postdata = {
    'entry': 'weibo',
    'gateway': '1',
    'from': '',
    'savestate': '7',
    'userticket': '1',
    'ssosimplelogin': '1',
    'vsnf': '1',
    'vsnval': '',
    'su': '',
    'service': 'miniblog',
    'servertime': '',
    'nonce': '',
    'pwencode': 'wsse',
    'sp': '',
    'encoding': 'UTF-8',
    'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META'
}

def get_servertime():

    url = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=dW5kZWZpbmVk&client=ssologin.js(v1.3.18)&_=1329806375939'
    data = urllib2.urlopen(url).read()
    p = re.compile('\((.*)\)')
    try:
        json_data = p.search(data).group(1)
        data = json.loads(json_data)
        servertime = str(data['servertime'])
        nonce = data['nonce']
        return servertime, nonce
    except:
        print 'Get severtime error!'
        return None

def get_pwd(pwd, servertime, nonce):
    pwd1 = hashlib.sha1(pwd).hexdigest()
    pwd2 = hashlib.sha1(pwd1).hexdigest()
    pwd3_ = pwd2 + servertime + nonce
    pwd3 = hashlib.sha1(pwd3_).hexdigest()
    return pwd3

def get_user(username):
    username_ = urllib.quote(username)
    username = base64.encodestring(username_)[:-1]
    return username

def main():
    username = 'yourweibo'#微博账号
    pwd = 'weibopw'#微博密码
    url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.18)'
    try:
        servertime, nonce = get_servertime()
    except:
        return
    global postdata
    postdata['servertime'] = servertime
    postdata['nonce'] = nonce
    postdata['su'] = get_user(username)
    postdata['sp'] = get_pwd(pwd, servertime, nonce)
    postdata = urllib.urlencode(postdata)
    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'}
    #其实到了这里,已经能够使用urllib2请求新浪任何的内容了,这里已经登陆成功了
    req  = urllib2.Request(
        url = url,
        data = postdata,
        headers = headers
    )
    result = urllib2.urlopen(req)
    text = result.read()
    #print text
    p = re.compile('location\.replace\(\"(.*?)\"\)') 
    try:
        login_url = p.search(text).group(1)
        print login_url
        #print login_url
        urllib2.urlopen(login_url)
        print "login success"
    except:
        print 'Login error!'
    #测试读取数据,下面的URL,可以换成任意的地址,都能把内容读取下来
    req = urllib2.Request(url='http://e.weibo.com/aj/mblog/mbloglist?page=1&count=15&max_id=3463810566724276&pre_page=1&end_id=3458270641877724&pagebar=1&_k=134138430655960&uid=2383944094&_t=0&__rnd=1341384513840',)
    result = urllib2.urlopen(req)
    text = result.read()
    print len(result.read())
    #unicode(eval(b),"utf-8")
    print eval("u'''"+text+"'''") 
main()

Write a Comment

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.7 Processed in 1ms