自作競馬予想AI+自動ブログ管理

競馬予想を載せます。

【Python】はてなブログへの自動投稿

はてなブログへの投稿の自動化について記載します。

本ブログの場合競馬の予想結果をpandasの機能を使ってHTMLのテーブルとして出力させていますのでそれを少し加工して記載させています。

下記よりコード

driver=webdriver.Chrome()
driver.get(weburl)
#ログインパスを設定
l_id=ログインID
l_pw=ログインPW

#id入力
login_id=driver.find_element_by_id('login-name')
login_id.send_keys(l_id)

#パスワードの入力
login_pw=driver.find_element_by_name('password')
login_pw.send_keys(l_pw)

# ログインボタンをクリック
button=driver.find_element_by_id('login-button')
button.click()
time.sleep(2)
 
# ここは独自です開催場所によってファイルを区切っているので
# そのディレクトリごとに対応しています。
for dir in files_dir:
    files = os.listdir(path+dir)

    #記事を書くボタンをクリック
    button=driver.find_element_by_class_name('admin-menu-edit-btn')
    button.click()
    time.sleep(2)
   
    # HTMLで編集をクリック
    driver.find_element_by_link_text("HTML編集").click()
    time.sleep(2)
   
    # タイトルを入力
    input_title=driver.find_element_by_id('title')
   # タイトルを加工
title = files[0]
    input_title.send_keys(title[:-9])
    time.sleep(2)
    
  # 本文を加工
    main = ''
    for file in files:
        with open(path+dir+'/'+fileencoding='utf-8'as f:
            html = f.read()
        main = main + '<h3>' + file[9:-5] + '</h3>'html
    # '''内容の入力'''
    'border="0"'
    main = main.replace('border="0"''')
    main = main.replace('style="text-align: right;"''')
    main = re.sub('\s\s+<''<'main)
  
    time.sleep(2)
  # 本文を入力
    input_text=driver.find_element_by_class_name('ace_text-input')
    input_text.send_keys(main)

    time.sleep(1)
    # '''「公開する」ボタンを押す'''
    button=driver.find_element_by_xpath('//*[@id="submit-button"]')
    button.click()