【Python】简单微博

发布于 2015-10-21  759 次阅读


1045063673.jpg

QQ截图20151021120241.jpg
QQ截图20151021120845.jpg
Github://github.com/ZanwingMak/simple_weibo

#coding:utf-8
import requests
from lxml import etree
import easygui
import msvcrt
import re
import urllib2
import json
import os
#UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
def login():
    try:
        username,password = easygui.multpasswordbox(u'请输入用户名和密码:',u'简单微博 - 登录',[u'用户名:',u'密码:'])
        url = 'http://login.weibo.cn/login/?ns=1&revalid=2&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt='
        my_session = requests.session()
        html = my_session.get(url).content
        selector = etree.HTML(html)
        pw = selector.xpath('//input[@type="password"]/@name')[0]
        vk = selector.xpath('//input[@name="vk"]/@value')[0]
        data = {
        'mobile': username,
        pw: password,
        'remember': 'on',
        'backURL': 'http%3A%2F%2Fweibo.cn%2F',
        'backTitle': u'微博',
        'tryCount': '',
        'vk': vk,
        'submit': u'登录'
        }
        html = my_session.post(url, data).content
        selector = etree.HTML(html)
        send_weibo(my_session,selector)
    except IndexError,e:
        easygui.msgbox(u'登录失败,用户名或密码错误,请重新登录。\n如多次输入仍登录失败,则是因为需要输入验证码的问题。\nReason:%s' % e)
    except Exception:
        #easygui.msgbox(u'程序出现了不可预料的问题,请重新运行程序。\n原因:%s' % e)
        pass
def send_weibo(my_session,selector):
    url = 'http://weibo.cn' + selector.xpath('/html/body/div[4]/form/@action')[0]
    while True:
        content = easygui.enterbox(u'请输入要发送的内容:',u'请填写')#.decode('GBK')
        if content == None:
            break
        data = {
        'rl': 0,
        'content': content
        }
        try:
            my_session.post(url, data)
            easygui.msgbox(u'发送成功')
        except:
            easygui.msgbox(u'发送失败,请重新输入。')
if __name__ == '__main__':
    login() #登录

❤动漫 | 音乐 | 游戏 萝莉赛高! 过膝袜赛高!