临时写的一个模拟直播平台用户登录,发弹幕的小demo,感兴趣的可以看看,参照下:
import sys
import random
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import os
import unittest
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
def login(browser):
try:
#模拟表单点击登录,需要自行处理安全验证的情况,这里模拟正常登录
browser.find_element_by_xpath('//*[@class="gui-navbar-login"]/a').click()
browser.switch_to_frame('_egame_login_frame_qq_')
sleep(2)
browser.switch_to_frame('ptlogin_iframe')
browser.find_element_by_id('switcher_plogin').click()
list_input = browser.find_element_by_xpath('//*[@id="loginform"]/div[1]/div/input')
list_input.send_keys('账号')
list_input = browser.find_element_by_xpath('//*[@id="loginform"]/div[2]/div/input')
list_input.send_keys('密码')
sleep(2)
browser.find_element_by_xpath('//*[@id="loginform"]/div[4]/a').click()
except Exception as result:
print('检测出异常{}'.format(result))
browser.close()
return 0
return 1
def showCookie(browser):
print("打印cookies")
cookies = browser.get_cookies()
cookie = [item["name"] + "=" + item["value"] for item in cookies]
cookiestr = '; '.join(item for item in cookie)
print (cookiestr)
http_ip = [
'119.101.117.134:9999',
'106.15.42.179:33543'
]
options = webdriver.ChromeOptions()
# 设置成中文
options.add_argument('lang=zh_CN.UTF-8')
# 添加头部
options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.103 Safari/537.36"')
#1允许所有图片;2阻止所有图片;3阻止第三方服务器图片
prefs = {
'profile.default_content_setting_values': {
'images': 2
}
}
options.add_experimental_option('prefs', prefs)
'''
PROXY = random.choice(http_ip)
print("PROXY->",PROXY)
#添加proxy参数
options.add_argument('--proxy-server=%s' % PROXY)
'''
try:
browser = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options)
except Exception as result:
print('检测出异常{}'.format(result))
# 设定页面加载限制时间
browser.set_page_load_timeout(20)
browser.maximize_window()
# 直播网页地址
url = "https://egame.qq.com/497383565"
try:
browser.get(url)
print("浏览器打开指定网页:",url)
except TimeoutException:
print('加载超过10秒,强制停止加载....')
#当页面加载时间超过设定时间,通过执行Javascript来stop加载,即可执行后续动作
browser.execute_script('window.stop()')
# 模拟登录操作
status = login(browser)
if status==1:
print("登录成功")
sleep(2)
browser.switch_to.default_content()
showCookie(browser)
try:
print("发送一条弹幕")
browser.get(url)
ipt = browser.find_element_by_xpath('//*[@class="chat-inputbox"]/input')
ipt.send_keys(u'666~')
sleep(1)
browser.find_element_by_xpath('//*[@class="chat-inputbox"]/i').click()
print("浏览器不支持时,点击播放")
browser.find_element_by_class_name('btn-autoPlay').click()
except Exception as result:
print('检测出异常{}'.format(result))
browser.close()
sleep(3)
try:
print('退出登录')
mouse = browser.find_element_by_class_name('nav-user')
ActionChains(browser).move_to_element(mouse).perform()
sleep(2)
browser.find_element_by_class_name('sign-out-btn').click()
except Exception as result:
print('检测出异常{}'.format(result))
showCookie(browser)
sleep(2)
print("关闭浏览器")
browser.close()
else:
print("登录失败")
网友评论