import requests import time import random from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains

定义评论内容

comment = """我爱坤,我爱坤 他是我的梦想,我的阳光 他的歌声,他的舞姿 让我心动,让我疯狂 我爱坤,我爱坤 他是我的偶像,我的英雄 他的笑容,他的温柔 让我沉醉,让我迷恋 我爱坤,我爱坤 他是我的一切,我的唯一 他的存在,他的付出 让我感动,让我感激 我爱坤,我爱坤 他是我的蔡徐坤"""

定义登录信息

username = "17584511015" password = "ccf1901304265"

创建浏览器对象

driver = webdriver.Chrome()

打开b站首页

driver.get("https://www.bilibili.com/")

点击登录按钮

login_button = driver.find_element_by_xpath( "//*[@id='i_cecream']/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div/div/span" ) login_button.click() time.sleep(1)

输入用户名和密码

username_input = driver.find_element_by_xpath("//input[@placeholder='请输入账号']") password_input = driver.find_element_by_xpath("//input[@placeholder='请输入密码']") username_input.send_keys(username) password_input.send_keys(password) time.sleep(0.1)

点击登录按钮

submit_button = driver.find_element_by_xpath("//div[@class='btn_primary ']") submit_button.click() time.sleep(15)

输入搜索关键词

search_input = driver.find_element_by_xpath("//input[@class='nav-search-input']") search_input.send_keys("蔡徐坤") time.sleep(0.1)

按回车键进行搜索

search_input.send_keys(Keys.ENTER) time.sleep(0.1)

获取当前窗口句柄

current_window = driver.current_window_handle

切换到新打开的窗口

for window_handle in driver.window_handles: if window_handle != current_window: driver.switch_to.window(window_handle) break

依次点击视频标题打开视频

elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") for i in range(len(elements)): try: # 在这里执行你想要的操作,例如获取元素文本、点击元素等 elements = driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") element = elements[i] print(element.text) # 打印元素文本 # 生成随机的点击延迟时间 delay = random.uniform(3, 5) time.sleep(delay) driver.execute_script("arguments[0].click();", element) # 使用execute_script方法点击元素 time.sleep(10)

    # 获取当前窗口句柄
    current_window = driver.current_window_handle
    # 切换到第三个窗口
    window_handle = driver.window_handles[2]
    driver.switch_to.window(window_handle)
    # 获取当前页面的URL
    current_url = driver.current_url
    #视频点赞
    video_like_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[1]/div[1]")
    video_like_button.click()
    time.sleep(2)
    #视频收藏
    video_collection_button =  driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[1]/div[3]")
    video_collection_button.click()
    time.sleep(2)
    video_collection1_button = driver.find_element_by_xpath("//*[@id='arc_toolbar_report']/div[3]/div/div/div[2]/div/ul/li/label")
    video_collection1_button.click()
    time.sleep(2)
    video_collection2_button = driver.find_element_by_xpath("//*[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button")
    video_collection2_button.click()
    time.sleep(2)
    # 编辑评论
    comment_input = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[2]/textarea")
    comment_input.click()
    comment_input.send_keys(comment)
    time.sleep(1)
    # 点击发送评论
    comment_button = driver.find_element_by_xpath("//*[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]")
    comment_button.click()
    time.sleep(5)
    #点赞评论
    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")
    like_button.click()
    time.sleep(5)
    # 关闭当前窗口,回到搜索结果页面
    driver.close()
    driver.switch_to.window(driver.window_handles[1])
except Exception as e:
    print(str(e))

关闭浏览器

driver.quit(


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

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