import\x20time import\x20random from\x20selenium\x20import\x20webdriver from\x20selenium.webdriver.common.keys\x20import\x20Keys from\x20selenium.webdriver.common.action_chains\x20import\x20ActionChains

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

#\x20定义登录信息 username\x20=\x20"17584511015" password\x20=\x20"ccf1901304265"

#\x20创建浏览器对象 driver\x20=\x20webdriver.Chrome()

#\x20打开b站首页 driver.get("https://www.bilibili.com/")

#\x20点击登录按钮 login_button\x20=\x20driver.find_element_by_xpath( \x20\x20"//*[@id='i_cecream']/div[2]/div[1]/div[1]/ul[2]/li[1]/li/div/div/span" ) login_button.click() time.sleep(1)

#\x20输入用户名和密码 username_input\x20=\x20driver.find_element_by_xpath("//input[@placeholder='请输入账号']") password_input\x20=\x20driver.find_element_by_xpath("//input[@placeholder='请输入密码']") username_input.send_keys(username) password_input.send_keys(password) time.sleep(0.1)

#\x20点击登录按钮 submit_button\x20=\x20driver.find_element_by_xpath("//div[@class='btn_primary '] ") submit_button.click() time.sleep(15)

#\x20输入搜索关键词 search_input\x20=\x20driver.find_element_by_xpath("//input[@class='nav-search-input']") search_input.send_keys("蔡徐坤") time.sleep(0.1)

#\x20按回车键进行搜索 search_input.send_keys(Keys.ENTER) time.sleep(0.1)

#\x20获取当前窗口句柄 current_window\x20=\x20driver.current_window_handle #\x20切换到新打开的窗口 for\x20window_handle\x20in\x20driver.window_handles: \x20\x20if\x20window_handle\x20!=\x20current_window: \x20\x20\x20driver.switch_to.window(window_handle) \x20\x20\x20break

#\x20依次点击视频标题打开视频 elements\x20=\x20driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") for\x20i\x20in\x20range(len(elements)): \x20\x20try: \x20\x20\x20#\x20在這裡執行你想要的操作,例如獲取元素文本、點擊元素等 \x20\x20\x20elements\x20=\x20driver.find_elements_by_xpath("//h3[@class='bili-video-card__info--tit']") \x20\x20\x20element\x20=\x20elements[i] \x20\x20\x20print(element.text)\x20#\x20打印元素文本 \x20\x20\x20#\x20生成随机的点击延迟时间 \x20\x20\x20delay\x20=\x20random.uniform(3, 5) \x20\x20\x20time.sleep(delay) \x20\x20\x20element.click()\x20#\x20点击元素 \x20\x20\x20time.sleep(10)

\x20\x20\x20#\x20获取当前窗口句柄 \x20\x20\x20current_window\x20=\x20driver.current_window_handle \x20\x20\x20#\x20切换到第三个窗口 \x20\x20\x20window_handle\x20=\x20driver.window_handles[2] \x20\x20\x20driver.switch_to.window(window_handle) \x20\x20\x20#\x20获取当前页面的URL \x20\x20\x20current_url\x20=\x20driver.current_url \x20\x20\x20#视频点赞 \x20\x20\x20video_like_button\x20=\x20driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[1]/div[1]") \x20\x20\x20video_like_button.click() \x20\x20\x20time.sleep(2) \x20\x20\x20#视频收藏 \x20\x20\x20video_collection_button\x20=\x20driver.find_element_by_xpath("//[@id='arc_toolbar_report']/div[1]/div[3]") \x20\x20\x20video_collection_button.click() \x20\x20\x20time.sleep(2) \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") \x20\x20\x20video_collection1_button.click() \x20\x20\x20time.sleep(2) \x20\x20\x20video_collection2_button\x20=\x20driver.find_element_by_xpath("//[@id ='arc_toolbar_report']/div[3]/div/div/div[3]/button") \x20\x20\x20video_collection2_button.click() \x20\x20\x20time.sleep(2) \x20\x20\x20#\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") \x20\x20\x20comment_input.click() \x20\x20\x20comment_input.send_keys(comment) \x20\x20\x20time.sleep(1) \x20\x20\x20#\x20点击发送评论 \x20\x20\x20comment_button\x20=\x20driver.find_element_by_xpath("//[@id ='comment']/div/div/div/div[2]/div[1]/div/div/div[3]") \x20\x20\x20comment_button.click() \x20\x20\x20time.sleep(5) \x20\x20\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") \x20\x20\x20like_button.click() \x20\x20\x20time.sleep(5) \x20\x20\x20#\x20关闭当前窗口,回到搜索结果页面 \x20\x20\x20driver.close() \x20\x20\x20driver.switch_to.window(driver.window_handles[1]) \x20\x20except\x20Exception\x20as\x20e: \x20\x20\x20print(str(e)) #\x20关闭浏览器 driver.quit()


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

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