xohozu
11/3/2014 - 7:55 AM

weibo.cn登录助手(Login assist for weibo.cn)

weibo.cn登录助手(Login assist for weibo.cn)

# coding=utf8

from pyquery import PyQuery as pq
import requests


class Weibo(object):

    """"Login Assist for weibo.cn"""

    def __init__(self, username, password):
        self.username = username
        self.password = password

        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2202.3 Safari/537.36'
        })

    def login(self):
        url = 'http://login.weibo.cn/login/'

        r = self.session.get(url)
        d = pq(r.text.encode('utf8'))

        action = d('form').attr.action

        data = {}
        inputs = d('form input')
        data[inputs[0].name] = self.username  # mobile | account name
        data[inputs[1].name] = self.password  # password
        for input in inputs[2:]:
            data[input.name] = input.value

        r = self.session.post(url + action, data=data)

    def get(self, url):
    	return self.session.get(url).text.encode('utf8')

if __name__ == '__main__':
    weibo = Weibo('user@example.com', 'password')
    weibo.login()
    print weibo.get('http://weibo.cn')