• selenium+python Douyu弹幕机器人


    #coding=utf-8
    
    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.support.ui import WebDriverWait
    import time
    import json
    import requests
    import sys
    
    class Robot:
        def __init__(self):
                self.browser = webdriver.Firefox()
    
    
        def login_with_code(self,usname,pwd):
    
            url = 'https://passport.douyu.com/index/login?passport_reg_callback=PASSPORT_REG_SUCCESS_CALLBACK&passport_login_callback=PASSPORT_LOGIN_SUCCESS_CALLBACK&passport_close_callback=PASSPORT_CLOSE_CALLBACK&passport_dp_callback=PASSPORT_DP_CALLBACK&type=login&client_id=1&state=https%3A%2F%2Fwww.douyu.com%2F2009&source=click_topnavi_login'
            self.browser.get(url)
            self.browser.maximize_window()
    
    
            button1 = WebDriverWait(self.browser,15).until(lambda x:x.find_element_by_class_name('js-qrcode-switch'))
            button1.click()
    
            button2 = WebDriverWait(self.browser,10).until(lambda x:x.find_element_by_xpath("//div[@class='loginbox-login-subtype']/span[@data-subtype='login-by-nickname']"))
            button2.click()
    
            input1 = WebDriverWait(self.browser,10).until(lambda x:x.find_element_by_name('username'))
            input1.send_keys(usname)
    
            input2 = WebDriverWait(self.browser,10).until(lambda x:x.find_element_by_name('password'))
            input2.send_keys(pwd)
    
            time.sleep(1)   #avoid the protection of douyu
    
            #button3 = self.browser.find_element_by_xpath("//div[@class='login-sbt-con']/input[@type='submit']")
            button3 = self.browser.find_element_by_class_name('btn-sub')
            button3.click()
    
    
            print('45...')
            time.sleep(15)
            print('30...')
            time.sleep(15)
            print('15...')
            time.sleep(12)
            print('3...')
            time.sleep(1)
            print('2...')
            time.sleep(1)
            print('1...')
            time.sleep(1)
    
            print ("Verification Code finish.......")
    
            while True:
                if(self.ifLogin()):
                    print('login success')
                    cookie_saved = self.browser.get_cookies()
                    fp = open('cookie.json','w')
                    json.dump(cookie_saved,fp)
                    fp.close()
                    break
                break
    
    
        def login_with_cookie(self,cookies):
            self.browser.get('https://www.douyu.com/directory/myFollow')
            self.browser.maximize_window()
            for cookie in cookies:
                self.browser.add_cookie(cookie)
            self.browser.refresh()
    
    
        def sendM(self,room,message):
    
            url = 'https://www.douyu.com/' + room
            self.browser.get(url)
    
            for i in range(10):    #连续发10条
                input_mes = WebDriverWait(self.browser,10).until(lambda x:x.find_element_by_class_name('ChatSend-txt'))
                input_mes.send_keys(message)
                time.sleep(0.4)
                button_send = WebDriverWait(self.browser,10).until(lambda y:y.find_element_by_class_name('ChatSend-button '))
                button_send.click()
                time.sleep(0.5) #最短发言时间
    
    
    
        def ifLogin(self):
            if 'PHPSESSID' in str(self.browser.get_cookies()):
                return True
            else:
                return False
    
    
    def main():
    
        username = 'XXXXXX'
        password = 'xxxxxx'
        room = '6388034'
        message = 'ceeeeeeeeeeeeeeb'
        robot = Robot()
    
        try:
            fp = open('cookie.json','r')
            cookies = json.load(fp)
            fp.close
            print("login with cookie...")
            robot.login_with_cookie(cookies)
        except:
            print("login with code...")
            robot.login_with_code(username,password)
        robot.sendM(room,message)
    
    if __name__ == '__main__':
        main()

    首次登陆自动使用账号密码登录,需要手动图片验证码及手机填写验证码,之后登录自动调用cookie。

    请勿用于非法用途

  • 相关阅读:
    struts2-dojo-plugin-2.3.1.2.jar!/struts-plugin.xml:29:119
    谈论高并发(十二)分析java.util.concurrent.atomic.AtomicStampedReference看看如何解决源代码CAS的ABA问题
    linux安装QQ
    Android:创建耐磨应用
    僵尸网络
    几个比较好的网站
    几点基于Web日志的Webshell检测思路
    Redis异常JedisConnectionException:Read timed out解决笔记
    ELk 几篇好的文章
    深入了解linux下的last命令及其数据源
  • 原文地址:https://www.cnblogs.com/Aiden-/p/11489465.html
Copyright © 2020-2023  润新知