Skip to content

Instantly share code, notes, and snippets.

@liaojie
Created November 23, 2013 11:15
Show Gist options
  • Save liaojie/7613385 to your computer and use it in GitHub Desktop.
Save liaojie/7613385 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf8 -*-
import urllib
import urllib2
import cookielib
import re
class V2EX:
def __init__(self):
self.loginurl = "http://v2ex.com/signin"
self.mission_url = 'http://www.v2ex.com/mission/daily'
self.username = your_user_name
self.passwd = your_password
def login(self):
cookie_support = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
regex = re.compile(r'<input type="hidden" value="(\d+)" name="once" />')
loginpage = urllib2.urlopen(self.loginurl).read()
once = regex.findall(loginpage)[0]
header = {
'Host': 'v2ex.com',
'Origin': 'http://v2ex.com',
'Referer': 'http://v2ex.com/signin'
}
postdata = urllib.urlencode({
'next': '/',
'u': self.username,
'p': self.passwd,
'once': once,
'next': '/'
})
req = urllib2.Request(
url='http://v2ex.com/signin',
data=postdata,
headers=header
)
response = urllib2.urlopen(req).read()
def sign(self):
mission_page = urllib2.urlopen(self.mission_url).read()
#print mission_page
if ("每日登录奖励已领取" in mission_page):
print "每日登录奖励已领取"
return
regex = re.compile(r"location.href = '(.*)'")
once = regex.findall(mission_page)[0]
onclick = 'http://www.v2ex.com' + once
response = urllib2.urlopen(onclick).read()
if __name__ == "__main__":
v2ex = V2EX()
v2ex.login()
v2ex.sign()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment