1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python + seleniumでwebデザインギャリーサイトの自動投稿 (1)環境構築

Posted at

概要

動機

副業の関連で治療院専門のwebデザインギャラリーサイトをwordpressで製作しようと思いたつ。でもギャラリーサイト画像用のキャプチャ保存、wordpressへのアップデートを毎回行うのがとても面倒。ということでpythonの勉強がてら自動投稿環境を作成。
長くなるので何回かに分けて投稿します。

* ここでいう自動投稿は、ターミナル上でpythonスクリプトを動かし、スクリーンキャプチャの取得から、投稿テキストの作成、worpdressへのログイン、投稿までを行えることを指します。

前提

  • mac OS Sierra
  • wordpress 4.7
  • python 3.5
  • selenium 3.0.2
  • beautifulsoup4 4.5.1

内容

  1. 環境構築
  2. 画面キャプチャの取得 - selenium
  3. 画像の切り抜き、圧縮 - PIL、tinypng
  4. 投稿用CSVの作成 - bs4
  5. 自動投稿 -selenium

実際のギャラリーサイトはこちらになります。

フォルダ構成

image_cap
├── cap.py #キャプチャ保存、csv書き込みのスクリプト
├── images #キャプチャ画像の保存先
├── min-images #圧縮したキャプチャ画像の保存先
├── list.txt #ギャラリ-に追加したいサイトのURLの取得
├── up-list.csv #一括投稿に使うCSV
├── upload.py #wordpress投稿用のスクリプト
├── setting.config #ログインID、パスワードやimageディレクトリのパスなどの設定

一応ソースコードをgithubで管理してます。
こちらからどうぞ。

1. 環境構築

ローカル側の設定

seleniumのインストール

キャプチャの取得等で使用するウェブプラウザをプログラムで動かすためのソフト。主にwebサイトのテスト用で使用されるみたい。

$ pip install selenium

chromewebdriverのダウンロード

今回はChromeを使いたいのでhomebrewよりダウンロード。

$ brew install chromedriver
$ which chromedriver 
  usr/local/bin/chromedriver # これをseleniumのpassとして設定します

beautifulsoup4のインストール

スクレイピングで掲載サイトのタイトルを取得するのに使います。

$ pip install beautifulsoup4

tinifyのインストール

画像の圧縮でtinypngのAPIを使用するために。こちらの投稿が大変参考になりました。API KEYの設定等も行ってください。

$ pip install tinify

lxmlのインストール

htmlパーサーとしてlxmlを利用するためにインストール。正直パーサーがどんなことするかイマイチわかっていない。。汗

pip install lxml

こちらを参考にさせて頂きました。

設定ファイルの読み込み

今後の修正で楽するためにpythonのConfigParserというのを使ってログインID、ファイルパスなどの変数をまとめました。
以下のコードではこちらを使っていきます。

setting.config
[CAPTURE] #キャプチャ画像のパス
img_pass = /cap_git/images
min_img_pass = /cap_git/min_image

[RESIZE] #tinypngのAPIキー
tiny_key = hogehoge

[WRITE] #csv書き込みの設定
init_row = post_title,post_category,capture,url,post_type,post_status
post_type = post
post_status = publish
up_address = http://test/wp-content/themes/test/dist/images/

[UPLOAD] #wordpressのログイン情報などなど
login_url = http://test/wp-login.php
login_id = hoge
login_pass = hogehoge
import_url = http://test/wp-admin/admin.php?import=csv
img_up_pass = http://test/wp-content/themes/sage-8.5/dist/images

それでcap.pyにおいて下記で変数を読み込みます

cap.py
# 設定ファイルの読み込み
conf = configparser.ConfigParser()
conf.read('setting.config')

# APIキーの設定
tinify.key = conf['RESIZE']['tiny_key']
# 縮小前の画像があるdir
FROM_DIR = conf['CAPTURE']['img_pass']
# 縮小後の画像を置くdir
MIN_DIR = conf['CAPTURE']['min_img_pass']
# 設定項目
INIT_ROW = list(conf['WRITE']['init_row'].split(','))
# 画像のURL
IM_ADRRESS = conf['WRITE']['up_address']
# 設定する投稿タイプ
POST_TYPE = conf['WRITE']['post_type']
POST_STATUS = conf['WRITE']['post_status']

configparser.ConfigParser()でモジュールの設定
conf.readeでconfgファイルのパスを指定して読み込み。
値には辞書型みたいな形式でアクセスできるのでそれを変数に挿入してます。

wordpress側の設定

Really Simple CSV Importerプラグインの追加

csvでの一括投稿を行うために。こちらを参考にしました。
その他、Advanced Custom Fieldsで投稿用のカスタムフィールドの設定もおこなっています。

その2画面キャプチャの保存へ続く

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?