selenium+python Douyu弹幕机器人

匿名 (未验证) 提交于 2019-12-02 22:51:30
#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 = self.browser.find_element_by_class_name('js-qrcode-switch')         button1.click()          button2 = self.browser.find_element_by_xpath("//div[@class='loginbox-login-subtype']/span[@data-subtype='login-by-nickname']")         button2.click()          input1 = self.browser.find_element_by_name('username')         input1.send_keys(usname)          input2 = self.browser.find_element_by_name('password')         input2.send_keys(pwd)          time.sleep(2)   #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)          input_mes = WebDriverWait(self.browser,10).until(lambda x:x.find_element_by_class_name('ChatSend-txt'))         input_mes.send_keys(message)          button_send = WebDriverWait(self.browser,10).until(lambda y:y.find_element_by_class_name('ChatSend-button '))         button_send.click()        def ifLogin(self):         if 'PHPSESSID' in str(self.browser.get_cookies()):             return True         else:             return False   def main():      username = 'XXXXXX'     password = 'XXXXXX'     room = '1256569'     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()

第一次登陆需要填写username,password,手机接收验证码手动填写,之后会记录下cookie,之后登录自动使用本地cookie。

之后再完善下细节。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!