一、首先,张贴其python脚本代码,从中可以看到,在安装python外,需要另外安装python的以下模块:requests,selenium和pyvirtualdisplay
#!-*- coding:utf-8 -*-from pyvirtualdisplay import Displayfrom selenium import webdriverimport requests#要签到的QQ群号列表groupid = [123456789,987654321]headers = {"Content-Type":"application/x-www-form-urlencoded","Host":"qiandao.qun.qq.com","Origin":"http://qiandao.qun.qq.com","Referer": "http://qiandao.qun.qq.com","User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"}#创建一个启动session的请求对象s = requests.Session()def login(): try: #启动一个虚拟桌面 display = Display(visible=0, size=(1100,700)) display.start() driver = webdriver.Firefox() driver.set_window_position(20, 40) driver.set_window_size(1100,700) #打开QQ登录界面 准备登录 driver.get('http://qzone.qq.com') driver.switch_to_frame('login_frame') driver.find_element_by_id('switcher_plogin').click() driver.find_element_by_id('u').clear() #填写QQ帐号 driver.find_element_by_id('u').send_keys('qq号') driver.find_element_by_id('p').clear() #填写QQ密码 driver.find_element_by_id('p').send_keys('****zh30.com****') #模拟点击登录按钮 执行登录 获取cookie driver.find_element_by_id('login_button').click() cookies = driver.get_cookies() #退出浏览器 driver.quit() display.stop() return cookies except: passcookies = login()for cookie in cookies: if cookie['name'] == 'skey': skey = cookie['value'] s.cookies.set(cookie['name'], cookie['value'])#计算bkn并存入cookiee = 5381for i in range(len(skey)): e = e + (e<<5)+ord(skey[i])bkn = str(2147483647 & e)s.cookies.set("Gtk", bkn)#签到方法def sign(groupid): for gid in groupid: response = s.post("http://qiandao.qun.qq.com/cgi-bin/sign", data={"gc":gid, "is_sign":0, "bkn":bkn}, headers=headers) responseJson = response.json() if responseJson.has_key('em') and responseJson['em'] == 'no login': break#开始签到sign(groupid)
二、安装python
1) 下载python3.3
~$ wget http://www.python.org/ftp/python/3.3.0/Python-3.3.0.tgz 2) 解压、进入目录、创建安装目录 ~$ tar -zxvf Python-3.3.0.tgz ~$ cd Python-3.3.0 ~$ mkdir /usr/local/python33 3) 编译、安装。(–prefix 指定安装到的位置;如果是centos5或其它版本,有可能在安装完后python中import zlib时会提示no module named zlib,需要在这条语句后面追加参数 –with-zlib) ~$ ./configure –prefix=/usr/local/python33 ~$ make && make install 4) 设置,因为安装完后 系统默认执行python命令时还是启动的2.7,我们需要把默认的2.7从bin目录中转移走,换成我们的python3.3。 ~$ mv /usr/bin/python /usr/bin/python27 ~$ ln -s /usr/local/python33/bin/python3.3 /usr/bin/python 以上两句 第一句 将原系统中的python重命令为python27,第二句创建软连接(快捷方式)指向到我们新安装的python3.3。这样在系统命令行中两个版本python都可以用。运行python将启动python3.3,运行python26将启动python2.7。三、安装pyhton的模块
ps:不知为什么,直接用pip安装了request,pyvirtualdisplay和selenium都不管用。所以改用源代码安装
1.安装requests模块
sudo git clone git://github.com/kennethreitz/requests.git cd requests/ sudo python setup.py install2.安装selenium
tar包下载地址: https://git.oschina.net/tanjiajun/python/repository/archive/master 上传到linux,解压,安装 sudo tar -zxvf selenium-3.0.2.tar.gz cd selenium-3.0.2 sudo python setup.py install 如果是pip安装的话,运行:sudo pip3 install selenium 3.安装pyvirtualdisplay tar包下载地址: https://git.oschina.net/tanjiajun/python/raw/master/PyVirtualDisplay-0.2.1.tar.gz 上传到linux,解压,安装 sudo tar -zxvf PyVirtualDisplay-0.2.1.tar.gz cd PyVirtualDisplay-0.2.1 sudo python setup.py install4.还有一个在selenium模块中,我们例子因为用到webdriver.Firefox()火狐浏览的driver.所以要下载geckodriverckod
下载地址:https://github.com/mozilla/geckodriver/releases
解压后将geckodriverckod,存放至/usr/local/bin/路径下即可sudo mv ./Downloads/geckodriver /usr/local/bin/
大功告成,运行我们开头的脚本代码,python sign.py(这边我保存的文件名为sign.py)