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

Google Colaboratory でTwitterの画像をダウンロード

Last updated at Posted at 2023-03-16

Google Colaboratory に chrome, selenium をインストール

chromium をインストールできるように

%%shell

cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF


apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg


cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

chromium、 driver、 selenium をインストール

!apt-get update
!apt-get install chromium chromium-driver
!pip install selenium

selenium 起動確認

ヘッドレスモードで起動を確認

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import requests
import io
from PIL import Image
from urllib.request import urlopen as uReq

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver', options=options)
driver.implicitly_wait(10)

ツイートから画像をダウンロード

Beautiful Soup ではimgまでアクセスできないのでこうする

url = "https://twitter.com/idolpicture2/status/1635762393920815105"
driver.get(url)

# article 内から img を検索
elems_article = driver.find_elements(By.TAG_NAME, 'article')
for elem_article in elems_article:
    imgs = elem_article.find_elements(By.TAG_NAME, 'img')
    for img in imgs:
        src = img.get_attribute('src')
        # ファイルの保存
        try:
          srcs = src.split("/")[-1].split('?')
          filename = "{}.{}".format( srcs[0], srcs[1].split('&')[0].replace('format=', '') )
          imagefile = open(filename, "wb")
          imagefile.write(uReq(src).read())
          imagefile.close()
        except Exception as e:
          print(e)

右側のディスクスペースに保存される

画像を Google Drive に保存する場合

こんなふうに Google Drive をマウントした後に画像を保存

import os
from google.colab import drive
drive.mount("/content/drive")
os.chdir("/content/drive/MyDrive/")

スクリーンショットを撮りたい場合

driver.save_screenshot("screenshot.png")

画像が取得できない場合

ログインしていないとツイートが見れないことがあります。
その時はこちらの通りにログインした後に画像を参照してみてください。

selenium から Twitter にログインして投稿

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?