{/'title/': /'Bilibili脚本测试 - 自动点赞、评论、弹幕发送工具/', /'description/': /'使用 Selenium 和 PySimpleGUI 开发的 B 站脚本,实现自动点赞视频、评论、发送弹幕等功能,帮助您高效管理 B 站账号。/', /'keywords/': /'Bilibili脚本, 自动点赞, 自动评论, 自动弹幕, B站脚本, Selenium, PySimpleGUI/', /'content/': /'import//u0020time//nfrom//u0020selenium//u0020import//u0020webdriver//nfrom//u0020selenium.webdriver.common.keys//u0020import//u0020Keys//nimport//u0020json//n//nimport//u0020PySimpleGUI//u0020as//u0020sg//n//ndef//u0020main()://n//t#//u0020创建窗口布局//n//tlayout//u0020=//u0020[//n//t/t[sg.Text('关键词',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#ff0000'),//u0020sg.Input(key='-KEYWORD-',//u0020size=(20,//u00201))],//n//t/t[sg.Text('评论内容',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#ff7f00'),//u0020sg.Input(key='-COMMENT-',//u0020size=(40,//u00201))],//n//t/t[sg.Text('弹幕内容',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#ffff00'),//u0020sg.Input(key='-BARRAGE-',//u0020size=(40,//u00201))],//n//t/t[sg.Checkbox('点赞视频',//u0020key='-LIKE_VIDEO-',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#00ff00')],//n//t/t[sg.Checkbox('收藏视频',//u0020key='-COLLECT_VIDEO-',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#00ffff')],//n//t/t[sg.Checkbox('发送评论',//u0020key='-SEND_COMMENT-',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#0000ff')],//n//t/t[sg.Checkbox('点赞评论',//u0020key='-LIKE_COMMENT-',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#8b00ff')],//n//t/t[sg.Text('弹幕循环次数',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#4b0082'),//u0020sg.Input(key='-BARRAGE_LOOP_TIMES-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('评论循环次数',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#000000'),//u0020sg.Input(key='-COMMENT_LOOP_TIMES-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('弹幕时间间隔',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#4b0082'),//u0020sg.Input(key='-BARRAGE_INTERVAL-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('评论时间间隔',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#000000'),//u0020sg.Input(key='-COMMENT_INTERVAL-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('每页点击的视频数量',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#4b0082'),//u0020sg.Input(key='-VIDEOS_PER_PAGE-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('翻页次数',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#000000'),//u0020sg.Input(key='-PAGE_COUNT-',//u0020size=(10,//u00201))],//n//t/t[sg.Text('点赞评论次数',//u0020font=('Helvetica',//u002012,//u0020'bold'),//u0020text_color='#8b00ff'),//u0020sg.Input(key='-LIKE_COMMENT_COUNT-',//u0020size=(10,//u00201))],//n//t/t[sg.Button('开始执行',//u0020font=('Helvetica',//u002012,//u0020'bold')),//u0020sg.Button('退出',//u0020font=('Helvetica',//u002012,//u0020'bold'))]//n//t]//n//n//t#//u0020创建窗口//n//twindow//u0020=//u0020sg.Window('Bilibili脚本测试',//u0020layout,//u0020background_image='background.jpg',//u0020text_color='#ffffff')//n//n//twhile//u0020True://n//t/tevent,//u0020values//u0020=//u0020window.read()//n//n//t/tif//u0020event//u0020==//u0020'开始执行'://n//t/t/tkeyword//u0020=//u0020values['-KEYWORD-']//n//t/t/tcomment//u0020=//u0020values['-COMMENT-']//n//t/t/tbarrage//u0020=//u0020values['-BARRAGE-']//n//t/t/tlike_video//u0020=//u0020values['-LIKE_VIDEO-']//n//t/t/tcollect_video//u0020=//u0020values['-COLLECT_VIDEO-']//n//t/t/tsend_comment//u0020=//u0020values['-SEND_COMMENT-']//n//t/t/tlike_comment//u0020=//u0020values['-LIKE_COMMENT-']//n//t/t/tbarrage_loop_times//u0020=//u0020int(values['-BARRAGE_LOOP_TIMES-'])//n//t/t/tcomment_loop_times//u0020=//u0020int(values['-COMMENT_LOOP_TIMES-'])//n//t/t/tbarrage_interval//u0020=//u0020int(values['-BARRAGE_INTERVAL-'])//n//t/t/tcomment_interval//u0020=//u0020int(values['-COMMENT_INTERVAL-'])//n//t/t/tvideos_per_page//u0020=//u0020int(values['-VIDEOS_PER_PAGE-'])//n//t/t/tpage_count//u0020=//u0020int(values['-PAGE_COUNT-'])//n//t/t/tlike_comment_count//u0020=//u0020int(values['-LIKE_COMMENT_COUNT-'])//n//n//t/t/t#//u0020使用cookie登录//n//t/t/tdriver//u0020=//u0020webdriver.Chrome()//n//t/t/tdriver.get('https://www.bilibili.com')//n//n//t/t/tf//u0020=//u0020open('cookie.txt',//u0020'r')//n//t/t/tlistcookie//u0020=//u0020json.loads(f.read())//u0020#//u0020读取文件中的cookies数据//n//n//t/t/tfor//u0020cookie//u0020in//u0020listcookie://n//t/t/t/tdriver.add_cookie(cookie)//u0020#//u0020将cookies数据添加到浏览器//n//t/t/tdriver.refresh()//u0020#//u0020刷新网页//n//n//t/t/t#//u0020输入搜索关键词//n//t/t/tsearch_input//u0020=//u0020driver.find_element_by_xpath(/'//input[@class='nav-search-input']/')//n//t/t/tsearch_input.send_keys(keyword)//n//t/t/ttime.sleep(0.1)//n//n//t/t/t#//u0020按回车键进行搜索//n//t/t/tsearch_input.send_keys(Keys.ENTER)//n//t/t/ttime.sleep(0.1)//n//n//t/t/t#//u0020获取当前窗口句柄//n//t/t/tcurrent_window//u0020=//u0020driver.current_window_handle//n//n//t/t/t#//u0020切换到新打开的窗口//n//t/t/tfor//u0020window_handle//u0020in//u0020driver.window_handles://n//t/t/t/tif//u0020window_handle//u0020!=//u0020current_window://n//t/t/t/t/tdriver.switch_to.window(window_handle)//n//t/t/t/t/tbreak//n//n//t/t/telements//u0020=//u0020None//n//t/t/tvideo_count//u0020=//u00200//u0020#//u0020记录点击的视频标题数量//n//t/t/tpage_num//u0020=//u00200//u0020#//u0020记录当前页数//n//n//t/t/twhile//u0020True://n//t/t/t/ttry://n//t/t/t/t/tif//u0020elements//u0020is//u0020None://n//t/t/t/t/t/t#//u0020第一次打开页面时获取视频标题元素列表//n//t/t/t/t/t/telements//u0020=//u0020driver.find_elements_by_xpath(/'//h3[@class='bili-video-card__info--tit']/')//n//t/t/t/t/telse://n//t/t/t/t/t/t#//u0020翻页后重新获取视频标题元素列表//n//t/t/t/t/t/telements//u0020=//u0020driver.find_elements_by_xpath(/'//h3[@class='bili-video-card__info--tit']/')//n//n//t/t/t/t/tif//u0020video_count//u0020>=//u0020videos_per_page://n//t/t/t/t/t/t#//u0020当所有视频标题都已点击过后,翻页//n//t/t/t/t/t/tvideo_count//u0020=//u00200//n//t/t/t/t/t/tpage_num//u0020+=//u00201//n//t/t/t/t/t/tif//u0020page_num//u0020>//u0020page_count://n//t/t/t/t/t/t/tbreak//n//n//t/t/t/t/t/tnext_page_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/'//[@id='i_cecream']/div/div[2]/div[2]/div/div/div/div[3]/div/div/button[10]/')//n//t/t/t/t/t/tnext_page_button.click()//n//t/t/t/t/t/ttime.sleep(1)//n//t/t/t/t/t/tcontinue//n//n//t/t/t/t/t#//u0020在这里执行你想要的操作,例如获取元素文本、点击元素等//n//t/t/t/t/telement//u0020=//u0020elements[video_count]//n//t/t/t/t/tprint(element.text)//u0020#//u0020打印元素文本//n//t/t/t/t/telement.click()//u0020#//u0020点击元素//n//t/t/t/t/ttime.sleep(1)//n//n//t/t/t/t/t#//u0020获取当前窗口句柄//n//t/t/t/t/tcurrent_window//u0020=//u0020driver.current_window_handle//n//t/t/t/t/t#//u0020切换到第三个窗口//n//t/t/t/t/twindow_handle//u0020=//u0020driver.window_handles[2]//n//t/t/t/t/tdriver.switch_to.window(window_handle)//n//t/t/t/t/t#//u0020获取当前页面的URL//n//t/t/t/t/tcurrent_url//u0020=//u0020driver.current_url//n//n//t/t/t/t/tif//u0020like_video://n//t/t/t/t/t/t#//u0020视频点赞//n//t/t/t/t/t/tvideo_like_button//u0020=//u0020driver.find_element_by_xpath(/'//[@id ='arc_toolbar_report']/div[1]/div[1]/')//n//t/t/t/t/t/tvideo_like_button.click()//n//t/t/t/t/t/ttime.sleep(1)//n//n//t/t/t/t/tif//u0020collect_video://n//t/t/t/t/t/t#//u0020��频收藏//n//t/t/t/t/t/tvideo_collection_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/'//[@id='arc_toolbar_report']/div[1]/div[3]/')//n//t/t/t/t/t/tvideo_collection_button.click()//n//t/t/t/t/t/ttime.sleep(3)//n//t/t/t/t/t/tvideo_collection1_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/'//[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label/')//n//t/t/t/t/t/tvideo_collection1_button.click()//n//t/t/t/t/t/ttime.sleep(1)//n//t/t/t/t/t/tvideo_collection2_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/'//[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button/')//n//t/t/t/t/t/tvideo_collection2_button.click()//n//t/t/t/t/t/ttime.sleep(2)//n//n//t/t/t/t/tfor//u0020_//u0020in//u0020range(barrage_loop_times)://n//t/t/t/t/t/tif//u0020barrage://n//t/t/t/t/t/t/t#//u0020编辑弹幕//n//t/t/t/t/t/t/tbarrage_input//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/t/'//[@id='bilibili-player']/div/div/div[1]/div[2]/div/div[2]/div[3]/div[1]/input/')//n//t/t/t/t/t/t/tbarrage_input.click()//n//t/t/t/t/t/t/tbarrage_input.send_keys(barrage)//n//t/t/t/t/t/t/ttime.sleep(2)//n//t/t/t/t/t/t/t#//u0020发送弹幕//n//t/t/t/t/t/t/tbarrage_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/t/'//[@id='bilibili-player']/div/div/div[1]/div[2]/div/div[2]/div[3]/div[2]/')//n//t/t/t/t/t/t/tbarrage_button.click()//n//t/t/t/t/t/t/ttime.sleep(barrage_interval)//n//n//t/t/t/t/tfor//u0020_//u0020in//u0020range(comment_loop_times)://n//t/t/t/t/t/tif//u0020send_comment://n//t/t/t/t/t/t/t#//u0020编辑评论//n//t/t/t/t/t/t/tcomment_input//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/t/'//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea/')//n//t/t/t/t/t/t/tcomment_input.click()//n//t/t/t/t/t/t/tcomment_input.send_keys(comment)//n//t/t/t/t/t/t/ttime.sleep(1)//n//t/t/t/t/t/t/t#//u0020点击发送评论//n//t/t/t/t/t/t/tcomment_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/t/'//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]/')//n//t/t/t/t/t/t/tcomment_button.click()//n//t/t/t/t/t/t/ttime.sleep(comment_interval)//n//t/t/t/t/t/t/tif//u0020like_comment_count//u0020>//u00200://n//t/t/t/t/t/t/t/t#//u0020点赞评论//n//t/t/t/t/t/t/t/tlike_button//u0020=//u0020driver.find_element_by_xpath(/n//t/t/t/t/t/t/t/t/t/'//[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i/')//n//t/t/t/t/t/t/t/tlike_button.click()//n//t/t/t/t/t/t/t/ttime.sleep(1)//n//t/t/t/t/t/t/t/tlike_comment_count//u0020-=//u00201//n//n//t/t/t/t/t#//u0020关闭当前窗口,回到搜索结果页面//n//t/t/t/t/tdriver.close()//n//t/t/t/t/tdriver.switch_to.window(driver.window_handles[1])//n//n//t/t/t/t/tvideo_count//u0020+=//u00201//n//t/t/t/t/texcept//u0020Exception//u0020as//u0020e://n//t/t/t/t/t/tprint(str(e))//n//t/t/t/t/t/tbreak//n//n//t/t/t#//u0020关闭浏览器//n//t/t/t/tdriver.quit()//n//n//t/telif//u0020event//u0020==//u0020'退出' or event == sg.WIN_CLOSED://n//t/t/tbreak//n//n//twindow.close()//n//n//nif//u0020__name__//u0020==//u0020'main'://n/tmain()/n}/


原文地址: https://www.cveoy.top/t/topic/pLjP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录