爬虫引发的思考:简书自动发送信息
2017-05-21 本文已影响451人
nonoBoy
这几天在写自动登录百度云盘的代码,希望把爬虫的数据自动存储到这个5T的云盘上去;其中运用到了selenium,因为之前抓淘宝商品数据的时候用过selenium,所以这次也采用熟悉的模块来实现简书的站内信(简信)发送,测试对象为我的好机油:尧妹;
主要解决的几个问题:
1.绕过登录时候的滑块验证码(之前研究过,正在尝试破解,大家好的方法给我留言):我采用的方法是点击微博,采用微博账号登录,哇咔咔--没有验证码了;
2.第二个问题是微博登录采用CSS_SELECTOR无法定位到user和password的输入框,测试发现需要用ID才能准确定位,额,CSS_SELECTOR其实有点坑,浪费我1个多小时(可以打两局王者了-..-||)
不说了上代码,大家采用自己的微博账号做测试,通过登陆后用webdirver执行js代码,能够做好多好好多事情呐~(比如:打开多窗口-此时浏览器里面已经有cookie,整个简书的任何操作都暴露在视野中哦,你可以每天自动实现日更,监控某些人的动作,及时评论等等等等;)
具体实现代码如下:
#!usr/bin/env python3.6
#-*- coding:utf-8 -*-
"""
@author:iBoy
@file: jianshu.py
@time: 2017/05/21
"""
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class autoJianshuMeg():
def sendMsg(self):
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get('https://www.jianshu.com/sign_in#/')
#click weibo button
submit2 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.weibo')))
submit2.click()
try:
input_name = wait.until(EC.presence_of_element_located((By.ID, 'userId'))) ##userId #
#don't forget to clear the textbox
input_name.clear()
time.sleep(3)
input_name.send_keys('youremail@gmail.com') #use your own userName
#CSS_SELECTOR doesn't work here
input_psw = wait.until(EC.presence_of_element_located((By.ID, 'passwd')))
input_psw.send_keys('your psassword') #use your own password
time.sleep(1)
login_submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.WB_btn_login')))
login_submit.click()
#now we login in successfully
#open a new window(label)
time.sleep(1)
js = 'window.open("http://www.jianshu.com/notifications#/chats/new?mail_to=4389953");' # the url to send msg
driver.execute_script(js)
#get handles
handles= driver.window_handles
#switch window
for handle in handles:
if handle != driver.current_window_handle: # I only opened two windows, so...the other one is the new one
print('switch to ', handle)
driver.switch_to_window(handle)
print(driver.current_window_handle) # you can see the current one
break
#get the textbox to send msg
mesg_input = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'form-control')))
time.sleep(1)
mesg_input.send_keys('HI~ 尧妹 这是机器人发来的消息...')
#get the button element of send msg
msg_send = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input.btn')))
msg_send.click()
except Exception as e:
print(e)
if __name__ == '__main__':
myJianshuMsg = autoJianshuMeg()
myJianshuMsg.sendMsg()
执行代码后,消息发送成功;

明天尧妹早上起来就会看到我的信息了,以后也可以不断地按时地提醒他给我的支付宝公益树去浇水了...