自動化:用selenium發一篇博文

萝卜薰發表於2024-06-07

1:Python的安裝與環境搭建

網址:https://www.python.org/
選擇“Downloads”下載最新的3.12版本的python,選擇一個路徑安裝(我選擇安裝在D盤根目錄下,新建了一個“python”資料夾),同時記得選擇 ‘Add Python3.12 to PATH”。可以透過開啟cmd輸入python檢查,如果返回了python的版本資訊,則說明路徑已經新增。

1.1 pycharm 的安裝

網址:https://www.jetbrains.com/pycharm/ 點選Download,跳轉至下載介面,向下滾動螢幕,找到pycharm community edition,點選下載。安裝時,可以放在任意位置,如圖勾選所有專案,最後點選安裝即可。
啟動pycharm 在彈出的彈框中選擇‘DO not import settings’,設定好ui,使用pycharm。

1.2 建立一個新的專案。


將python專案建立在桌面下,並命名為‘pythonProject’。interpreter選項選擇已經存在的直譯器,選擇安裝在D盤下的‘python.exe’檔案

1.3 selenium庫下載

可以再pycharm下方選擇“terminal”,輸入pip install selenium==3.14.0

1.4 Chrome 驅動器下載

先開啟裝置中已有的Chrome瀏覽器,透過"關於Google Chrome" 找到瀏覽器的版本,再從網上下載對應版本的驅動器:chromedriver.exe。比如我的瀏覽器是125版本,那麼驅動器也要搜尋125版本的驅動器來下載。記得把驅動器安裝在之前建立的‘pythonProject’資料夾裡。

2.0 selenium程式碼編寫

由於部落格園有反爬措施,本程式碼需要先手動登入部落格園賬號,然後重新開啟瀏覽器,再用selenium接管這個瀏覽器開啟部落格園,實現自動發博。
這裡的開啟瀏覽器不同於正常的點選引用程式,需要將Chrome的本地路徑加入到環境變數中,具體操作方法為:“win+s” -> 搜尋並點選“編輯系統環境變數” ->在‘高階’頁面找到並點選‘環境變數’ -> 選擇‘path’,點選‘編輯’ -> 點選“新建”,並將Chrome的地址貼上進來,最後點選“確定”儲存。
然後開啟cmd 輸入“chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"”即可開啟Chrome,之後將由selenium接管這個已開啟的Chrome頁面。

2.1 selenium程式碼部分

`from selenium import webdriver
import time
path = 'chromedriver.exe'
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option('debuggerAddress', "127.0.0.1:9222")
browser = webdriver.Chrome(path, chrome_options=chrome_options)
url = 'https:cnblogs.com'
browser.get(url)
time.sleep(2)

my_blog = browser.find_element_by_id('myblog_icon')#點選我的部落格
my_blog.click()

new_blog = browser.find_element_by_id('blog_nav_newpost')#點選 新隨筆
new_blog.click()
time.sleep(1)

ipt_title = browser.find_element_by_id('post-title') # 找到標題輸入框
ipt_title.send_keys('部落格的標題')

txt_input = browser.find_element_by_id('md-editor') # 找到正文輸入框
txt_input.send_keys(‘部落格的正文’)
post_btn = browser.find_element_by_xpath('//button[@class="cnb-button d-inline-flex align-items-center ng-star-inserted"]')#找到 釋出 按鈕
post_btn.click() #點選發布`

現在就可以透過selenium自動發一篇部落格了。

3.0透過呼叫文心一言的API介面來詢問HTTPX的使用方法:

可以直接參考https://blog.csdn.net/dream_of_grass/article/details/135535369
需要建立應用獲得API Key 和Secret Key,然後可以透過付費或者代金券購買一個付費介面,我購買的是ERNIE-3.5-8K,接著將API Key 和Secret Key 代入下列程式碼中(以下程式碼僅適用於ERNIE-3.5-8K介面):
`import requests
import json
def get_access_token():
"""
使用 API Key,Secret Key 獲取access_token,替換下列示例中的應用API Key、應用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=m7aPGWXAAiTD1oHEcqdaGFDf&client_secret=52qMJ3zqQHNlMRQwhhpiTiCErD5ieFF0"
payload = json.dumps("")
headers = {'Content-Type': 'application/json','Accept': 'application/json'}
response = requests.request("POST", url, headers=headers, data=payload)
return response.json().get("access_token")

def main():
url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + get_access_token()
payload = json.dumps({"messages": [{"role": "user", "content": "如何使用httpx?"}])
headers = {'Content-Type': 'application/json'}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

if name == 'main':
main()`

執行程式碼即可得到答案。

4.0自動發一篇關於“呼叫文心一言API詢問HTTPX的使用方法”的部落格

修改一下2.1部分中的“部落格標題”和“部落格正文”,將3.0的程式碼複製貼上到部落格正文內,即可釋出。

相關文章