Bilibili 脚本测试 - 自动点赞、评论、弹幕
import\u0020time from\u0020selenium\u0020import\u0020webdriver from\u0020selenium.webdriver.common.keys\u0020import\u0020Keys import\u0020json
import\u0020PySimpleGUI\u0020as\u0020sg
def\u0020main(): \t#\u0020创建窗口布局 \tlayout\u0020=\u0020[ \t [sg.Text('关键词'),\u0020sg.Input(key='-KEYWORD-',\u0020size=(20,\u00201))], \t [sg.Text('评论内容'),\u0020sg.Input(key='-COMMENT-',\u0020size=(40,\u00201))], \t [sg.Text('弹幕内容'),\u0020sg.Input(key='-BARRAGE-',\u0020size=(40,\u00201))], \t [sg.Checkbox('点赞视频',\u0020key='-LIKE_VIDEO-')], \t [sg.Checkbox('收藏视频',\u0020key='-COLLECT_VIDEO-')], \t [sg.Checkbox('发送评论',\u0020key='-SEND_COMMENT-')], \t [sg.Checkbox('点赞评论',\u0020key='-LIKE_COMMENT-')], \t [sg.Text('弹幕循环次数'),\u0020sg.Input(key='-BARRAGE_LOOP_TIMES-',\u0020size=(10,\u00201))], \t [sg.Text('评论循环次数'),\u0020sg.Input(key='-COMMENT_LOOP_TIMES-',\u0020size=(10,\u00201))], \t [sg.Text('弹幕时间间隔'),\u0020sg.Input(key='-BARRAGE_INTERVAL-',\u0020size=(10,\u00201))], \t [sg.Text('评论时间间隔'),\u0020sg.Input(key='-COMMENT_INTERVAL-',\u0020size=(10,\u00201))], \t [sg.Text('每页点击的视频数量'),\u0020sg.Input(key='-VIDEOS_PER_PAGE-',\u0020size=(10,\u00201))], \t [sg.Text('翻页次数'),\u0020sg.Input(key='-PAGE_COUNT-',\u0020size=(10,\u00201))], \t [sg.Text('点赞评论次数'),\u0020sg.Input(key='-LIKE_COMMENT_COUNT-',\u0020size=(10,\u00201))], \t [sg.Button('开始执行'),\u0020sg.Button('退出')] \t]
\t#\u0020创建窗口 \twindow\u0020=\u0020sg.Window('Bilibili脚本测试',\u0020layout,\u0020background_color='#F0EDD8')
\t#\u0020设置字体颜色切换列表 \tcolor_list\u0020=\u0020['red',\u0020'green',\u0020'blue',\u0020'yellow',\u0020'purple'] \tcolor_index\u0020=\u00200
\twhile\u0020True: \t event,\u0020values\u0020=\u0020window.read()
\t if\u0020event\u0020==\u0020'开始执行': \t keyword\u0020=\u0020values['-KEYWORD-'] \t comment\u0020=\u0020values['-COMMENT-'] \t barrage\u0020=\u0020values['-BARRAGE-'] \t like_video\u0020=\u0020values['-LIKE_VIDEO-'] \t collect_video\u0020=\u0020values['-COLLECT_VIDEO-'] \t send_comment\u0020=\u0020values['-SEND_COMMENT-'] \t like_comment\u0020=\u0020values['-LIKE_COMMENT-'] \t barrage_loop_times\u0020=\u0020int(values['-BARRAGE_LOOP_TIMES-']) \t comment_loop_times\u0020=\u0020int(values['-COMMENT_LOOP_TIMES-']) \t barrage_interval\u0020=\u0020int(values['-BARRAGE_INTERVAL-']) \t comment_interval\u0020=\u0020int(values['-COMMENT_INTERVAL-']) \t videos_per_page\u0020=\u0020int(values['-VIDEOS_PER_PAGE-']) \t page_count\u0020=\u0020int(values['-PAGE_COUNT-']) \t like_comment_count\u0020=\u0020int(values['-LIKE_COMMENT_COUNT-'])
\t #\u0020使用cookie登录 \t driver\u0020=\u0020webdriver.Chrome() \t driver.get('https://www.bilibili.com')
\t f\u0020=\u0020open('cookie.txt',\u0020'r') \t listcookie\u0020=\u0020json.loads(f.read())\u0020#\u0020读取文件中的cookies数据
\t for\u0020cookie\u0020in\u0020listcookie: \t driver.add_cookie(cookie)\u0020#\u0020将cookies数据添加到浏览器 \t driver.refresh()\u0020#\u0020刷新网页
\t #\u0020输入搜索关键词 \t search_input\u0020=\u0020driver.find_element_by_xpath("//input[@class='nav-search-input']") \t search_input.send_keys(keyword) \t time.sleep(0.1)
\t #\u0020按回车键进行搜索 \t search_input.send_keys(Keys.ENTER) \t time.sleep(0.1)
\t #\u0020获取当前窗口句柄 \t current_window\u0020=\u0020driver.current_window_handle
\t #\u0020切换到新打开的窗口 \t for\u0020window_handle\u0020in\u0020driver.window_handles: \t if\u0020window_handle\u0020!=\u0020current_window: \t driver.switch_to.window(window_handle) \t break
\t elements\u0020=\u0020None \t video_count\u0020=\u00200\u0020#\u0020记录点击的视频标题数量 \t page_num\u0020=\u00200\u0020#\u0020记录当前页数
\t while\u0020True: \t try: \t if\u0020elements\u0020is\u0020None: \t #\u0020第一次打开页面时获取视频标题元素列表 \t elements\u0020=\u0020driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") \t else: \t #\u0020翻页后重新获取视频标题元素列表 \t elements\u0020=\u0020driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']")
\t if\u0020video_count\u0020>=\u0020videos_per_page: \t #\u0020当所有视频标题都已点击过后,翻页 \t video_count\u0020=\u00200 \t page_num\u0020+=\u00201 \t if\u0020page_num\u0020>\u0020page_count: \t break
\t next_page_button\u0020=\u0020driver.find_element_by_xpath( \t "//*[@id='i_cecream']/div/div[2]/div[2]/div/div/div/div[3]/div/div/button[10]") \t next_page_button.click() \t time.sleep(1) \t continue
\t #\u0020在这里执行你想要的操作,例如获取元素文本、点击元素等 \t element\u0020=\u0020elements[video_count] \t print(element.text)\u0020#\u0020打印元素文本 \t element.click()\u0020#\u0020点击元素 \t time.sleep(1)
\t #\u0020获取当前窗口句柄 \t current_window\u0020=\u0020driver.current_window_handle \t #\u0020切换到第三个窗口 \t window_handle\u0020=\u0020driver.window_handles[2] \t driver.switch_to.window(window_handle) \t #\u0020获取当前页面的URL \t current_url\u0020=\u0020driver.current_url
\t if\u0020like_video: \t #\u0020视频点赞 \t video_like_button\u0020=\u0020driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[1]/div[1]") \t video_like_button.click() \t time.sleep(1)
\t if\u0020collect_video: \t #\u0020��频收藏 \t video_collection_button\u0020=\u0020driver.find_element_by_xpath( \t "//[@id='arc_toolbar_report']/div[1]/div[3]") \t video_collection_button.click() \t time.sleep(3) \t video_collection1_button\u0020=\u0020driver.find_element_by_xpath( \t "//[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label") \t video_collection1_button.click() \t time.sleep(1) \t video_collection2_button\u0020=\u0020driver.find_element_by_xpath( \t "//*[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button") \t video_collection2_button.click() \t time.sleep(2)
\t for\u0020_\u0020in\u0020range(barrage_loop_times): \t if\u0020barrage: \t #\u0020编辑弹幕 \t barrage_input\u0020=\u0020driver.find_element_by_xpath( \t "//[@id='bilibili-player']/div/div/div[1]/div[2]/div/div[2]/div[3]/div[1]/input") \t barrage_input.click() \t barrage_input.send_keys(barrage) \t time.sleep(2) \t #\u0020发送弹幕 \t barrage_button\u0020=\u0020driver.find_element_by_xpath( \t "//[@id='bilibili-player']/div/div/div[1]/div[2]/div/div[2]/div[3]/div[2]") \t barrage_button.click() \t time.sleep(barrage_interval)
\t for\u0020_\u0020in\u0020range(comment_loop_times): \t if\u0020send_comment: \t #\u0020编辑评论 \t comment_input\u0020=\u0020driver.find_element_by_xpath( \t "//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea") \t comment_input.click() \t comment_input.send_keys(comment) \t time.sleep(1) \t #\u0020点击发送评论 \t comment_button\u0020=\u0020driver.find_element_by_xpath( \t "//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]") \t comment_button.click() \t time.sleep(comment_interval) \t if\u0020like_comment_count\u0020>\u00200: \t #\u0020点赞评论 \t like_button\u0020=\u0020driver.find_element_by_xpath( \t "//*[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i") \t like_button.click() \t time.sleep(1) \t like_comment_count\u0020-=\u00201
\t #\u0020关闭当前窗口,回到搜索结果页面 \t driver.close() \t driver.switch_to.window(driver.window_handles[1])
\t video_count\u0020+=\u00201 \t except\u0020Exception\u0020as\u0020e: \t print(str(e)) \t break
\t #\u0020关闭浏览器 \t driver.quit()
\t elif\u0020event\u0020==\u0020'退出' or event == sg.WIN_CLOSED: \t break
\twindow.close()
if\u0020__name__ == 'main': main()
原文地址: https://www.cveoy.top/t/topic/pLjF 著作权归作者所有。请勿转载和采集!