原理:通过爬虫,模拟人的浏览器操作删除,因此需要在电脑端通过浏览器操作。
下面是用来自动删除自己所有回答的Python程序,需要安装selenium库:
# 使用的浏览器为火狐,需要下载geckodriver并添加到系统PATH
# 删除一些之后可能会报错停止,重新运行即可
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from random import random
driver = webdriver.Firefox()
driver.get("https://www.zhihu.com/")
xxx = input("登录后回车继续 >>>")
driver.get("https://www.zhihu.com/creator/manage/creation/answer")
xxx = input("加载完成后回车继续 >>>")
# 循环执行删除操作
while (True) :
# xxx = input("下一个 >>>")
# if (xxx == "q"):
# break
# 点击“更多”
more = driver.find_element(By.CSS_SELECTOR, r'button[id^="Popover"][id$="-toggle"][class="Button CreationCard-ActionButton css-r3cz0w Button--grey Button--withIcon Button--withLabel"]').click()
sleep(1 + 1*random())
for i in range(5, 14):
try:
# 点击“删除”
delete = driver.find_element(By.CSS_SELECTOR, f"button.Button:nth-child({i})")
if (delete.text == "删除"):
delete.click()
except:
pass
sleep(1 + 1*random())
# 点击“确认”
sure = driver.find_element(By.CSS_SELECTOR, r".Button--primary").click()
sleep(1 + 1*random())