四、代码实现
from selenium import webdriver
import time
# 导入类库
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=C:\Users\lxs\AppData\Local\Google\Chrome\User Data")
driver=webdriver.Chrome(options=options)
driver.get('https://live.kuaishou.com/u/......?csr=true')#打开一个页面
time.sleep(2) #缓两秒是让网页能到加载完全
for i in range(0,10): #循环10次,输入10次六六六
#通过chrome的css选择器
input = driver.find_element_by_css_selector('#app > div.live-detail > div.liveroom-sidebar.light > div.chat > div.chat-actions > div.chat-input > div > textarea')
input.send_keys('六六六') #向元素输入3个六
submit = driver.find_element_by_css_selector('#app > div.live-detail > div.liveroom-sidebar.light > div.chat > div.chat-actions > div.chat-input > div > button')
submit.click() #点击操作
除了以上注释外,还应注意css选择器的使用:
按F12,选择左上角的选项,就能在页面中直接选中需要的元素了,如下操作:右键copy->selector(因为代码就是用这种模式选择的)
五、运行代码
运行结果:
程序自己打开chrome,访问这个正在直播的直播间,这里的六就是程序自己敲上去的。根据代码,总共循环输入提交10次。
总结:
因为真正模拟登陆暂时没有写出来,所以只能通过这种方法来实现,速度也慢。真正做到模拟登陆就会快很多倍,这种方式适用于无法实现模拟登陆的情况。
以上就是安装chrome并通过selenium实现快手自动弹幕的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
更多相关阅读请进入《Python》频道 >>

Python编程 从入门到实践 第2版
python入门书籍,非常畅销,超高好评,python官方公认好书。