Created
April 16, 2015 06:05
-
-
Save huzhifeng/ebfbd399f2ef12c18256 to your computer and use it in GitHub Desktop.
Splinter自动登录QQ空间并发表说说
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
#encoding=utf-8 | |
import sys | |
import time | |
from splinter import Browser | |
browser = Browser() | |
# Visit http://m.qzone.com will redirect to http://ui.ptlogin2.qzone.com/cgi-bin/login? | |
# url = 'http://m.qzone.com' | |
url = 'http://ui.ptlogin2.qzone.com/cgi-bin/login?style=9&appid=549000929&pt_ttype=1&s_url=http%3A%2F%2Fm.qzone.com%2Finfocenter%3Fg_f%3D' | |
print(u'打开起始页面 %s' % (url)) | |
browser.visit(url) | |
guideSkip = browser.find_by_id('guideSkip') | |
if guideSkip: | |
print(u'访问触屏版...') | |
guideSkip.click() | |
qq = sys.argv[1] if len(sys.argv) > 1 else 'your qq' | |
password = sys.argv[2] if len(sys.argv) > 2 else 'your password' | |
print(u'%s 登录中...' % (qq)) | |
browser.fill('u', qq) | |
browser.fill('p', password) | |
browser.find_by_id('go').click() | |
if browser.is_text_present(u'说点什么吧'): | |
print(u'登录成功') | |
else: | |
print(u'登录失败') | |
browser.quit() | |
sys.exit() | |
print(u'定位输入框...') | |
# u'说点什么吧' | |
textarea = browser.find_by_xpath("//button[@class='txt-area']") or browser.find_by_css('txt-area') | |
if not textarea: | |
print(u'没有找到输入框') | |
browser.quit() | |
sys.exit() | |
textarea.click() | |
print(u'激活输入框...') | |
# u'写点什么吧…' | |
textarea = browser.find_by_tag('textarea') or browser.find_by_xpath("//textareada") | |
if not textarea: | |
print(u'激活输入框失败') | |
browser.quit() | |
sys.exit() | |
print(u'发表说说...') | |
now = time.strftime('%a, %Y/%m/%d %H:%M:%S', time.localtime()) | |
textarea.fill(u'Published at %s' % now) | |
submit = browser.find_by_id('form-0-submit') or browser.find_by_value(u'发表') | |
if not submit: | |
print(u'没有找到发表按钮') | |
browser.quit() | |
sys.exit() | |
submit.click() | |
browser.quit() | |
print(u'程序结束') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment