3
1

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 1 year has passed since last update.

闇のプログラミングAdvent Calendar 2021

Day 16

Pythonで監視対象のスクショを撮影して監視する

Last updated at Posted at 2021-12-15

こんな画像を自動で撮る

  • 画像証跡は日本のSEの基本

20211210_142034_0_shuumai.png

環境構築

  • Seleniumを環境に合わせて構築する
  • OCI無料枠でやる場合Oracle Linuxはゴミだったのでオススメしない

コード

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from io import BytesIO
from datetime import datetime
import functools
from PIL import Image
import sys

# speedy log
print = functools.partial(print, flush=True)


def fanSarvice(accountName):
    # file name
    now = datetime.now()
    nowDate = now.strftime('%Y%m%d_%H%M%S')
    # selenium parametor
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--window-size=1080,1920')
    # 環境に合わせてchromedriverとchrome.exeの位置を調整する
    # Linux/Mac
    # options.binary_location = '/usr/bin/google-chrome'
    # driver = webdriver.Chrome('chromedriver', options=options)
    # Windows
    options.binary_location = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
    driver = webdriver.Chrome(
        options=options, executable_path="C:\\dev\\chromedriver.exe")
    # profile
    profile = f'https://twitter.com/{accountName}'
    # popular reply
    searchMore = f'https://twitter.com/search?q=to%3A{accountName}&src=typed_query'
    # recent reply
    recentSearch = f'https://twitter.com/search?q=to%3A{accountName}&src=typed_query&f=live'
    # image
    withImagesTweet = f'https://twitter.com/search?q=from%3A{accountName}%20filter%3Aimages&src=typed_query&f=live'
    getSereenShotList = [profile, searchMore, recentSearch, withImagesTweet]
    print(f'list is {getSereenShotList}')

    # main
    for i, j in enumerate(getSereenShotList):
        print(i, j)
        driver.get(j)
        time.sleep(10)
        im = Image.open(BytesIO(driver.get_screenshot_as_png()))
        crpim = im.crop((120, 0, 740, 1920))
        crpim.save(f'{nowDate}_{i}_{accountName}.png', quality=95)

    driver.quit()


if __name__ == "__main__":
    accountName = sys.argv[1]
    fanSarvice(accountName)

実行

python fanScreenShot.py shuumai 

今後

  • サンプルのURLは悪意が強めなので、悪意を弱めたり、無くしたり、さらに強めたり、はたまたTwitter以外にしたてもっと縦長にしてたくさん撮る等の応用ができる
  • 悪以外ではUIテスト等業務でも色々使いまわせる
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?