在b站转发了500多条抽奖动态了,一次都没中。
500+条手动删太麻烦了,然后就写了个程序自动删除动态,分享一下。一起学习
[Python] 纯文本查看 复制代码 from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
class Bili(object):
driver_path = r"D:\ProgramApp\chromedriver_win32\chromedriver.exe"
def __init__(self):
self.driver = webdriver.Chrome(executable_path=Bili.driver_path)
self.login_url = "https://passport.bilibili.com/login"
self.home_url = "https://www.bilibili.com/"
self.space_url = "https://space.bilibili.com"
def _login(self):
self.driver.get(self.login_url)
WebDriverWait(self.driver, timeout=1000).until(EC.url_to_be(self.home_url))
print("登陆成功")
self._to_space()
def _to_space(self):
self.driver.get(self.space_url)
space_url_id = self.driver.current_url
dynamic_url = space_url_id + '/dynamic'
for i in range(500):
self.driver.get(dynamic_url)
WebDriverWait(self.driver, timeout=30).until(EC.presence_of_all_elements_located((By.XPATH, "//*[@id='page-dynamic']/div[1]/div/div/div[1]/div[3]/p")))
self.driver.find_element_by_xpath("//*[@id='page-dynamic']/div[1]/div/div/div[1]/div[2]/div").click()
time.sleep(1)
del_btn = self.driver.find_element_by_xpath("//*[@id='page-dynamic']/div[1]/div/div/div[1]/div[3]/p")
del_btn.click()
time.sleep(2)
ok_btn = self.driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div[2]/div[2]/button[1]")
ok_btn.click()
print("已删除%d条" % (i + 1))
def run(self):
self._login()
if __name__ == '__main__':
bili = Bili()
bili.run() |