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