一鍵自動化博客發佈工具,用過的人都說好(csdn篇)

CSDN應該是大家接觸到最多的博客平臺了,所以一款能夠發佈到CSDN的自動化工具還是非常有必要的。

今天給大家講講自動化CSDN博客發佈的思路和一些問題的解決辦法。

解決問題的思路一定是最重要的,知識是死的,問題是活的,如何在工作中解決遇到的問題是我們需要面臨的大問題。

前提條件

前提條件當然是先下載 blog-auto-publishing-tools這個博客自動發佈工具,地址如下:https://github.com/ddean2009/blog-auto-publishing-tools

CSDN的實現

csdn的文章編輯頁面進入很簡單,在你已經登錄的情況下,直接訪問https://editor.csdn.net/md/就可以進入他的博客發佈頁面了。

具體實現的代碼在publisher/csdn_publisher.py中。

標題

csdn的標題部分,沒有ID,也沒有name,只有一個孤零零的input。

那麼我們怎麼找到這個元素呢?

一個常用的辦法是通過xpath和placeholder來定位到這個input元素。

image-20240508101945309

    # 文章標題
    title = driver.find_element(By.XPATH, '//div[contains(@class,"article-bar")]//input[contains(@placeholder,"請輸入文章標題")]')
    title.clear()
    if 'title' in front_matter['title'] and front_matter['title']:
        title.send_keys(front_matter['title'])
    else:
        title.send_keys(common_config['title'])
    time.sleep(2)  # 等待3秒

文章內容

csdn的文章內容部分也是動態變動的,不是一個固定的textarea,但是看了它的代碼,用的也不是常見的CodeMirror,我猜應該是自己實現的一個動態編輯器。

不過沒關係,萬變不離其宗。

既然不用使用send_keys來添加內容,我們就是用複製和拷貝大法來實現這個功能。

    # 文章內容 markdown版本
    file_content = read_file_with_footer(common_config['content'])
    # 用的是CodeMirror,不能用元素賦值的方法,所以我們使用拷貝的方法
    cmd_ctrl = Keys.COMMAND if sys.platform == 'darwin' else Keys.CONTROL
    # 將要粘貼的文本內容複製到剪貼板
    pyperclip.copy(file_content)
    action_chains = webdriver.ActionChains(driver)
    content = driver.find_element(By.XPATH, '//div[@class="editor"]//div[@class="cledit-section"]')
    content.click()
    time.sleep(2)
    # 模擬實際的粘貼操作
    action_chains.key_down(cmd_ctrl).send_keys('v').key_up(cmd_ctrl).perform()
    time.sleep(3)  # 等待3秒

拷貝就是通用的流程了。

但是拷貝之前,我們需要先定位到拷貝的地址。

這裏我用的是xpath定位到editor class下面的cledit-section。

定位之後,按下click按鈕,然後直接粘貼內容即可。

發佈文章按鈕

內容都輸入好之後,我們就可以點擊右邊的發佈文章按鈕了。

csdn的按鈕沒有id,所以我們還是得使用xpath來定位到這個button。

    # 發佈文章
    send_button = driver.find_element(By.XPATH, '//button[contains(@class, "btn-publish") and contains(text(),"發佈文章")]')
    send_button.click()
    time.sleep(2)

點擊發布文章後,會有一個彈窗框:

image-20240508105432619

這個彈出框裏面是需要填寫的一些額外信息。比如文章標籤,添加封面,文章摘要,分類專欄,文章類型和可見範圍等等內容。

文章標籤

添加文章標籤的路徑有點複雜。

image-20240508110312590

首先我們點擊添加文章標籤按鈕,這時候又會彈出一個對話框。

在這個對話框裏面,我們需要文字搜索框,輸入tag,然後回車,然後繼續輸入tag,繼續回車。

做完所有的操作之後,還需要點擊右上角的x關閉按鈕,把這個彈出框關閉。

    # 文章標籤
    if 'tags' in front_matter and front_matter['tags']:
        tags = front_matter['tags']
    else:
        tags = csdn_config['tags']
    if tags:
        add_tag = driver.find_element(By.XPATH,
                                        '//div[@class="mark_selection"]//button[@class="tag__btn-tag" and contains(text(),"添加文章標籤")]')
        add_tag.click()
        time.sleep(1)
        tag_input = driver.find_element(By.XPATH, '//div[@class="mark_selection_box"]//input[contains(@placeholder,"請輸入文字搜索")]')
        for tag in tags:
            tag_input.send_keys(tag)
            time.sleep(2)
            tag_input.send_keys(Keys.ENTER)
            time.sleep(1)

        # 關閉按鈕
        close_button = driver.find_element(By.XPATH, '//div[@class="mark_selection_box"]//button[@title="關閉"]')
        close_button.click()
        time.sleep(1)

添加封面

CSDN的封面會自動檢測文章內容中的圖片,把這些圖片設置爲文章的封面。

當然我們也可以自行設置。

    if 'image' in front_matter and front_matter['image']:
        file_input = driver.find_element(By.XPATH, "//input[@type='file']")
        # 文件上傳不支持遠程文件上傳,所以需要把圖片下載到本地
        file_input.send_keys(download_image(front_matter['image']))
        time.sleep(2)

要注意的是,這裏的image地址是在markdown文件中的yaml front matter中設置的。

如圖所示:

image-20240507154807745

設置摘要

csdn的摘要部分也沒有ID,還是需要通過xpath來進行獲取。

這裏通過textarea的placeholder來進行獲取。

    # 摘要
    if 'description' in front_matter['description'] and front_matter['description']:
        summary = front_matter['description']
    else:
        summary = common_config['summary']
    if summary:
        summary_input = driver.find_element(By.XPATH, '//div[@class="desc-box"]//textarea[contains(@placeholder,"摘要:會在推薦、列表等場景外露")]')
        summary_input.send_keys(summary)
        time.sleep(2)

分類專欄

csdn的分類專欄需要提前創建好。

每個專欄都是一個checkbox,我們可以通過checkbox的value來定位到這個專欄選項。

實現代碼如下:

    # 分類專欄
    categories = csdn_config['categories']
    if categories:
        for category in categories:
            category_input = driver.find_element(By.XPATH, f'//input[@type="checkbox" and @value="{category}"]/..')
            category_input.click()
            time.sleep(1)

可見範圍

最後可以設置的就是文字的可見範圍了。

可見範圍是有id的,我們可以根據id來獲取到這個input,然後點擊他。

    # 可見範圍
    visibility = csdn_config['visibility']
    if visibility:
        visibility_input = driver.find_element(By.ID, visibility)
        visibility_input.click()

最後的發佈

最後,我們終於可以發佈了。

csdn的發佈按鈕也沒有id,我們只能通過對應的class和button的text內容來定位到發佈按鈕。

    # 發佈
    if auto_publish:
        publish_button = driver.find_element(By.XPATH, '//div[@class="modal__button-bar")]//button[contains(text(),"發佈文章")]')
        publish_button.click()

總結

CSDN的實現還是比較複雜的,因爲涉及到的東西比較多。大家可以好好琢磨琢磨。 點我查看更多精彩內容:www.flydean.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章