python模拟登录知乎

# -*- coding:utf-8 -*-
import urllib
import urllib2
import cookielib
import sys
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding("utf-8")
class login():
    zhihuurl="https://www.zhihu.com/#signin"
    def __init__(self):
        self.loginurl="https://www.zhihu.com/login/email"
        self.data={"_xsrf":"d9d05c3ecd636d3515482ee011e040c9","password":"xxxx","remember_me":"true","email":"791962385@qq.com"}
        self.postdata=urllib.urlencode(self.data)
        self.cj=cookielib.CookieJar()
        self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
    def getinfo(self):
        request=urllib2.Request(url=self.loginurl,data=self.postdata)
        result=self.opener.open(request)
        result2=self.opener.open(self.zhihuurl)
        print result2.read()
if __name__ == '__main__':
    test=login()
    test.getinfo()

 

posted @ 2016-03-23 15:01  小卒子0624  阅读(212)  评论(0编辑  收藏  举报