0


Python使用Selenium库实现CSDN自动化发帖

    虽然CSDN上有很多优秀的作品,但也不乏很多很水的文章,我也不知道这种有什么意义。不过发这么水的文章多没意思,让浏览器自动化发帖就行了。以下程序能够实现CSDN自动化发帖,同时附自动给关注的人的文章点赞的程序。因为也有很多作者之间互赞,但不看文章的质量,评论区也都是水军,不说该文章具体的问题,而只是泛泛的夸赞。

    以下给出完整代码,需要自己调试。我就不详细介绍程序了,比较简单。主要难点是iframe和window不断切换,需要检查HTML源代码检查。

完整代码

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
# from selenium.webdriver.edge.options import Options
class Config:
    def __init__(self):
        # 账号
        self.username = ''
        # 密码
        self.password = ''
        

def get(conf, driver, url):
    driver.maximize_window()
    driver.get(url)
    # 最多等待5秒使页面加载进来,隐式等待
    driver.implicitly_wait(5)
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[text()='登录']"))).click()
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(0))
    # driver.switch_to.frame(0)
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='密码登录']"))).click()
    username = driver.find_element(By.XPATH, "//input[@autocomplete='username']")
    username.send_keys(conf.username)
    # time.sleep(1)
    passwd = driver.find_element(By.XPATH, "//input[@autocomplete='current-password']")
    passwd.send_keys(conf.password)
    div = driver.find_element(By.CLASS_NAME,'inform-title')
    div.find_element(By.TAG_NAME, "i").click()
    driver.find_element(By.XPATH, "//*[text()='登录']").click()
    time.sleep(1)
    driver.switch_to.window(driver.window_handles[0])
    # time.sleep(1)
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(0))
    # driver.switch_to.frame(0)
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, "btn-skip").click()
    # WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "btn-skip"))).click()

    driver.switch_to.window(driver.window_handles[0])
    # time.sleep(1)
    
    driver.find_element(By.CSS_SELECTOR, "a[href='https://mp.csdn.net']").click()
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "btn-close"))).click()
    driver.find_element(By.CSS_SELECTOR, "a.content").click()
    time.sleep(1)
    
    driver.find_element(By.XPATH, "//span[text()='创作助手']").click()
    time.sleep(1)
    driver.switch_to.frame(1)
    box = driver.find_element(By.CLASS_NAME, "i-ai-chat-welcome-questions")
    # print(list.get_attribute("outerHTML"))
    list = box.find_elements(By.TAG_NAME, "button")
    writetest(driver,list)
    # list = driver.find_elements(By.CLASS_NAME,"sub-people-list-box" )
    # print(len(list))
    # thumbs_up(driver,list)

i = 0
def writetest(driver,list):
    global i
    li = list[i]
    i = i + 1
    title_tag = li.find_element(By.CSS_SELECTOR, "span.ivu-typography")
    title_text = title_tag.text
    li.click()
    WebDriverWait(driver,15).until(EC.presence_of_element_located((By.CSS_SELECTOR,"button.ivu-btn.ivu-btn-text.ivu-mr-32")))
    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "svg[width='12'][height='12']"))).click()
    # text_1 = '"重新生成 "'
    # WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,f"//span[text()='{text_1}']")))
    # text_2 = '"复制"'
    # WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,f"//p[text()='{text_2}']]"))).click()
    
    # driver.find_element(By.XPATH, "//img[@src='https://mp-blog.csdn.net/mp_blog/img/ceditlose.12eeb4a2.png']").click()
    driver.switch_to.window(driver.window_handles[0])
    driver.find_element(By.CSS_SELECTOR,"img.edit-title-close").click()
    title = driver.find_element(By.TAG_NAME,"textarea")
    title.send_keys(title_text)
    WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(0))
    # driver.switch_to.frame(0)
    content = driver.find_element(By.TAG_NAME,"p")
    # content.click()
    content.send_keys(Keys.CONTROL, 'v')
    driver.switch_to.window(driver.window_handles[0])
    # box = driver.find_element(By.CSS_SELECTOR, "div.btn-box")
    # buttons = box.find_elements(By.TAG_NAME,"button")
    # buttons[1].click()
    driver.find_element(By.XPATH,"//span[text()=' 发布博客']").click()
    # for li in list:
        # li.find_element(By.CSS_SELECTOR, "span.ivu-typography")
    
    
def thumbs_up(driver,list):
    i = 3
    n = 1
    j = 0
    for people in list:
        driver.switch_to.window(driver.window_handles[0])

        if i > 0:
            
            img = people.find_element(By.TAG_NAME, "img")
            img.click()
            time.sleep(2)  # 等待页面加载
            if driver is None:
                print("Driver is None. Exiting.")
            driver.switch_to.window(driver.window_handles[n+j])

            n = n + 1
            time.sleep(2)  # 等待页面加载
            try:
                article = driver.find_element(By.TAG_NAME, "article")

                article.find_element(By.TAG_NAME, "img").click()
                j = j + 1
                time.sleep(2)
                driver.switch_to.window(driver.window_handles[-1])
                # 找到要操作的元素
                element = driver.find_element(By.ID, "is-like-imgactive-new")
                tag = driver.find_element(By.ID, "blog-digg-num")
                # 初始显示状态
                print(f"初始 display 属性: {element.value_of_css_property('display')}")

                if element.value_of_css_property('display') == 'none':
                    tag.click()
                    # driver.execute_script("arguments[0].style.display = 'block';", element)
                # else:
                #     driver.execute_script("arguments[0].style.display = 'none';", element)
                time.sleep(2)
                print(f" display 属性: {element.value_of_css_property('display')}")
            except:
                pass
                
            i -= 1

if __name__ == '__main__':
    conf = Config()

    url = 'https://blog.csdn.net/2301_80892630?type=sub&spm=1010.2135.3001.5348'
    driver = webdriver.Edge()
    get(conf, driver, url)
    time.sleep(10)
    driver.quit()
标签: selenium

本文转载自: https://blog.csdn.net/2301_80892630/article/details/142993736
版权归原作者 wdxylb 所有, 如有侵权,请联系我们删除。

“Python使用Selenium库实现CSDN自动化发帖”的评论:

还没有评论