B站蔡徐坤视频自动点赞、收藏、评论脚本
import requests\nimport time\nimport random\nfrom bs4 import BeautifulSoup\nfrom selenium import webdriver\nfrom selenium.webdriver.common.keys import Keys\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.common.action_chains import ActionChains\n\n# 定义评论内容\ncomment = """我爱坤,我爱坤 他是我的梦想,我的阳光 他的歌声,他的舞姿 让我心动,让我疯狂\n我爱坤,我爱坤 他是我的偶像,我的英雄 他的笑容,他的温柔 让我沉醉,让我迷恋\n我爱坤,我爱坤 他是我的一切,我的唯一 他的存在,他的付出 让我感动,让我感激\n我爱坤,我爱坤 他是我的蔡徐坤"""\n\n# 定义登录信息\nusername = "17584511015"\npassword = "ccf1901304265"\n\n# 创建浏览器对象\ndriver = webdriver.Chrome()\n\n# 打开b站首页\ndriver.get("https://www.bilibili.com/")\n\n# 点击登录按钮\nlogin_button = driver.find_element_by_xpath("//[@id='i_cecream']/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div/div/span")\nlogin_button.click()\ntime.sleep(1)\n\n# 输入用户名和密码\nusername_input = driver.find_element_by_xpath("//input[@placeholder='请输入账号']")\npassword_input = driver.find_element_by_xpath("//input[@placeholder='请输入密码']")\nusername_input.send_keys(username)\npassword_input.send_keys(password)\ntime.sleep(0.1)\n\n# 点击登录按钮\nsubmit_button = driver.find_element_by_xpath("//div[@class='btn_primary ']")\nsubmit_button.click()\ntime.sleep(15)\n\n# 输入搜索关键词\nsearch_input = driver.find_element_by_xpath("//input[@class='nav-search-input']")\nsearch_input.send_keys("蔡徐坤")\ntime.sleep(0.1)\n\n# 按回车键进行搜索\nsearch_input.send_keys(Keys.ENTER)\ntime.sleep(0.1)\n\n# 获取当前窗口句柄\ncurrent_window = driver.current_window_handle\n# 切换到新打开的窗口\nfor window_handle in driver.window_handles:\n if window_handle != current_window:\n driver.switch_to.window(window_handle)\n break\n\n# 依次点击视频标题打开视频\nelements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']")\nfor i in range(len(elements)):\n try:\n # 在这里执行你想要的操作,例如获取元素文本、点击元素等\n elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']")\n element = elements[i]\n print(element.text) # 打印元素文本\n # 生成随机的点击延迟时间\n delay = random.uniform(3, 5)\n time.sleep(delay)\n driver.execute_script("arguments[0].click();", element) # 使用execute_script方法点击元素\n time.sleep(10)\n\n # 获取当前窗口句柄\n current_window = driver.current_window_handle\n # 切换到第三个窗口\n window_handle = driver.window_handles[2]\n driver.switch_to.window(window_handle)\n # 获取当前页面的URL\n current_url = driver.current_url\n #视频点赞\n video_like_button = driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[1]/div[1]")\n video_like_button.click()\n time.sleep(2)\n #视频收藏\n video_collection_button = driver.find_element_by_xpath("//[@id='arc_toolbar_report']/div[1]/div[3]")\n video_collection_button.click()\n time.sleep(2)\n video_collection1_button = driver.find_element_by_xpath("//[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")\n video_collection1_button.click()\n time.sleep(2)\n video_collection2_button = driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")\n video_collection2_button.click()\n time.sleep(2)\n # 编辑评论\n comment_input = driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")\n comment_input.click()\n comment_input.send_keys(comment)\n time.sleep(1)\n # 点击发送评论\n comment_button = driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")\n comment_button.click()\n time.sleep(5)\n #点赞评论\n like_button = driver.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 like_button.click()\n time.sleep(5)\n # 关闭当前窗口,回到搜索结果页面\n driver.close()\n driver.switch_to.window(driver.window_handles[1])\n except Exception as e:\n print(str(e))\n# 关闭浏览器\ndriver.quit()
原文地址: https://www.cveoy.top/t/topic/pyCY 著作权归作者所有。请勿转载和采集!