Created
December 3, 2013 15:28
-
-
Save saltcoffee/7771133 to your computer and use it in GitHub Desktop.
豆瓣登录发状态
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
# date:2013.12.1 | |
import urllib | |
import urllib2 | |
import re | |
import cookielib | |
import time | |
class LoginDouban(object): | |
"""docstring for ClassName""" | |
def __init__(self, email, password): | |
#super(ClassName, self).__init__() | |
#self.response = response | |
self.url = 'http://www.douban.com/accounts/login' | |
cookie = cookielib.CookieJar() | |
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) | |
self.data = { | |
"form_email":email, | |
"form_password":password, | |
"source":"index_nav" | |
} | |
self.response = self.opener.open(self.url, urllib.urlencode(self.data)) | |
def login_douban(self): | |
if self.response.geturl() == self.url: | |
html = self.response.read() | |
regex = re.compile(r'<img id="captcha_image" src="(.+?)" alt="captcha" class="captcha_image"/>') | |
imgurl = regex.findall(html) | |
res = urllib.urlretrieve(imgurl[0], 'gg.jpg') | |
#获取captcha-id参数 | |
captcha = re.search('<input type="hidden" name="captcha-id" value="(.+?)"/>' ,html) | |
if captcha: | |
vcode=raw_input('请输入图片上的验证码:') | |
self.data["captcha-solution"] = vcode | |
self.data["captcha-id"] = captcha.group(1) | |
self.data["user_login"] = "登录" | |
#验证码 | |
self.response = self.opener.open(self.url, urllib.urlencode(self.data)) | |
#登录成功 | |
if self.response.geturl() == "http://www.douban.com/": | |
print 'login success !' | |
def post_douban(self): | |
html = self.response.read() | |
#print html | |
ckcode = re.findall(r'<a href="http://www.douban.com/accounts/logout?.+ck=(.*?)">', html) | |
print ckcode | |
dic = { | |
"ck":ckcode[0], | |
"comment":"hahaha" | |
} | |
self.response = self.opener.open("http://www.douban.com/?", urllib.urlencode(dic)) | |
if self.response.geturl() == "http://www.douban.com/": | |
print 'post success !' | |
email = raw_input("Email:") | |
password = raw_input("Password:") | |
m = LoginDouban(email, password) | |
m.login_douban() | |
m.post_douban() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment