Bilibili 蔡徐坤视频自动化点赞、收藏、评论脚本
import\x20requests\nimport\x20time\nimport\x20random\nfrom\x20bs4\x20import\x20BeautifulSoup\nfrom\x20selenium\x20import\x20webdriver\nfrom\x20selenium.webdriver.common.keys\x20import\x20Keys\nfrom\x20selenium.webdriver.common.by\x20import\x20By\nfrom\x20selenium.webdriver.common.action_chains\x20import\x20ActionChains\n\n#\x20定义评论内容\ncomment\x20=\x20""" 我爱坤,我爱坤\x20他是我的梦想,我的阳光\x20他的歌声,他的舞姿\x20让我心动,让我疯狂\n我爱坤,我爱坤\x20他是我的偶像,我的英雄\x20他的笑容,他的温柔\x20让我沉醉,让我迷恋\n我爱坤,我爱坤\x20他是我的一切,我的唯一\x20他的存在,他的付出\x20让我感动,让我感激\n我爱坤,我爱坤\x20他是我的蔡徐坤\n"""\n\n#\x20定义登录信息\nusername\x20=\x20"17584511015"\npassword\x20=\x20"ccf1901304265"\n\n#\x20创建浏览器对象\ndriver\x20=\x20webdriver.Chrome()\n\n#\x20打开b站首页\ndriver.get("https://www.bilibili.com/")\n\n#\x20点击登录按钮\nlogin_button\x20=\x20driver.find_element_by_xpath("\x20//[@id='i_cecream']/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div/div/span"\x20)\nlogin_button.click()\ntime.sleep(1)\n\n#\x20输入用户名和密码\nusername_input\x20=\x20driver.find_element_by_xpath("//input[@placeholder='请输入账号']")\npassword_input\x20=\x20driver.find_element_by_xpath("//input[@placeholder='请输入密码']")\nusername_input.send_keys(username)\npassword_input.send_keys(password)\ntime.sleep(0.1)\n\n#\x20点击登录按钮\nsubmit_button\x20=\x20driver.find_element_by_xpath("//div[@class='btn_primary ']")\nsubmit_button.click()\ntime.sleep(15)\n\n#\x20输入搜索关键词\nsearch_input\x20=\x20driver.find_element_by_xpath("//input[@class='nav-search-input']")\nsearch_input.send_keys("蔡徐坤")\ntime.sleep(0.1)\n\n#\x20按回车键进行搜索\nsearch_input.send_keys(Keys.ENTER)\ntime.sleep(0.1)\n\n#\x20获取当前窗口句柄\ncurrent_window\x20=\x20driver.current_window_handle\n#\x20切换到新打开的窗口\nfor\x20window_handle\x20in\x20driver.window_handles:\n\x20\x20if\x20window_handle\x20!=\x20current_window:\n\x20\x20\x20\x20driver.switch_to.window(window_handle)\n\x20\x20\x20\x20break\n\n#\x20依次点击视频标题打开视频\nelements\x20=\x20driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']")\nfor\x20i\x20in\x20range(len(elements)):\n\x20\x20try:\n\x20\x20\x20\x20#\x20在这里执行你想要的操作,例如获取元素文本、点击元素等\n\x20\x20\x20\x20elements\x20=\x20driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']")\n\x20\x20\x20\x20element\x20=\x20elements[i]\n\x20\x20\x20\x20print(element.text)\x20#\x20打印元素文本\n\x20\x20\x20\x20#\x20生成随机的点击延迟时间\n\x20\x20\x20\x20delay\x20=\x20random.uniform(3,\x205)\n\x20\x20\x20\x20time.sleep(delay)\n\x20\x20\x20\x20driver.execute_script("arguments[0].click();",\x20element)\x20#\x20使用execute_script方法点击元素\n\x20\x20\x20\x20time.sleep(10)\n\n\x20\x20\x20\x20#\x20获取当前窗口句柄\n\x20\x20\x20\x20current_window\x20=\x20driver.current_window_handle\n\x20\x20\x20\x20#\x20切换到第三个窗口\n\x20\x20\x20\x20window_handle\x20=\x20driver.window_handles[2]\n\x20\x20\x20\x20driver.switch_to.window(window_handle)\n\x20\x20\x20\x20#\x20获取当前页面的URL\n\x20\x20\x20\x20current_url\x20=\x20driver.current_url\n\x20\x20\x20\x20#视频点赞\n\x20\x20\x20\x20video_like_button\x20=\x20driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[1]/div[1]")\n\x20\x20\x20\x20video_like_button.click()\n\x20\x20\x20\x20time.sleep(2)\n\x20\x20\x20\x20#视频收藏\n\x20\x20\x20\x20video_collection_button\x20=\x20driver.find_element_by_xpath("//[@id='arc_toolbar_report']/div[1]/div[3]")\n\x20\x20\x20\x20video_collection_button.click()\n\x20\x20\x20\x20time.sleep(2)\n\x20\x20\x20\x20video_collection1_button\x20=\x20driver.find_element_by_xpath("//[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")\n\x20\x20\x20\x20video_collection1_button.click()\n\x20\x20\x20\x20time.sleep(2)\n\x20\x20\x20\x20video_collection2_button\x20=\x20driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")\n\x20\x20\x20\x20video_collection2_button.click()\n\x20\x20\x20\x20time.sleep(2)\n\x20\x20\x20\x20#\x20编辑评论\n\x20\x20\x20\x20comment_input\x20=\x20driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")\n\x20\x20\x20\x20comment_input.click()\n\x20\x20\x20\x20comment_input.send_keys(comment)\n\x20\x20\x20\x20time.sleep(1)\n\x20\x20\x20\x20#\x20点击发送评论\n\x20\x20\x20\x20comment_button\x20=\x20driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")\n\x20\x20\x20\x20comment_button.click()\n\x20\x20\x20\x20time.sleep(5)\n\x20\x20\x20\x20#点赞评论\n\x20\x20\x20\x20like_button\x20=\x20driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[2]/div[1]/div[2]/div[2]/div[3]/div/span[2]/i")\n\x20\x20\x20\x20like_button.click()\n\x20\x20\x20\x20time.sleep(5)\n\x20\x20\x20\x20#\x20关闭当前窗口,回到搜索结果页面\n\x20\x20\x20\x20driver.close()\n\x20\x20\x20\x20driver.switch_to.window(driver.window_handles[1])\n\x20\x20except\x20Exception\x20as\x20e:\n\x20\x20\x20\x20print(str(e))\n#\x20关闭浏览器\ndriver.quit()
原文地址: https://www.cveoy.top/t/topic/pyC0 著作权归作者所有。请勿转载和采集!