0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

EC2でスクレイピング備忘録

Last updated at Posted at 2024-07-06

Amazon Linux 2023のみ下記手順でインストール可能です。
Amazon Linux 2 AMIでは
/opt/google/chrome/google-chrome: symbol lookup error: /opt/google/chrome/google-chrome: undefined symbol: cupsUserAgent
と言われて、インストールできません。

  1. google-chrome.repoを作成して、下記を記載する

    sudo emacs /etc/yum.repos.d/google-chrome.repo 
    
    ------------------------------------------------------------------
    [google-chrome]
    name=google-chrome
    baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
    enabled=1
    gpgcheck=1
    gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
    
    [google-chrome]
    name=google-chrome
    baseurl=https://dl.google.com/linux/chrome/rpm/stable/x86_64
    enabled=1
    gpgcheck=1
    gpgkey=https://dl.google.com/linux/linux_signing_key.pub 
    ------------------------------------------------------------------
    
  2. Chromeをインストールする
    t2インスタンスでは下記

    sudo yum install --enablerepo=google-chrome google-chrome-stable
    google-chrome --version
    >> Google Chrome 126.0.6478.126
    
    or
    
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    sudo yum install ./google-chrome-stable_current_x86_64.rpm
    

    t3インスタンスでは下記

    sudo yum install google-chrome-stable.x86_64 --nogpgcheck
    google-chrome --version
    >> Google Chrome 126.0.6478.126
    

    Google Chromeは公式にはx86_64アーキテクチャ(Intel/AMDプロセッサ)向けに提供されています。したがって、t4gインスタンス(ARM64アーキテクチャ)に直接インストールするのは難しいらしい

  3. chromedriverをダウンロードして、固定のフォルダに置く
    URLは対応するドライバーのURLへ変更する。下記リンクから確認できる。
    https://developer.chrome.com/docs/chromedriver/downloads?hl=ja

    wget https://storage.googleapis.com/chrome-for-testing-public/126.0.6478.126/linux64/chromedriver-linux64.zip
    sudo unzip chromedriver-linux64.zip
    cd chromedriver-linux64/
    sudo mv chromedriver /usr/local/bin
    
  4. 日本語フォントをインストール

    sudo yum install ipa-pgothic-fonts.noarch
    
  5. Pythonライブラリをインストール

    pip3 install selenium
    pip3 install PyInstaller webdriver_manager
    pip3 install chromedriver-binary==117.0.5938.149.0
    
  6. Pythonコードを実行

    sample.py
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    
    service = webdriver.chrome.service.Service(executable_path='/usr/local/bin/chromedriver')
    driver = webdriver.Chrome(service=service, options=options)
    
    driver.get('https://www.google.co.jp')
    element_text=driver.find_element(By.CLASS_NAME,"MV3Tnb").text
    
    print(element_text)
    
    driver.quit()
    

出力は Googleについて

補足

インストールに失敗した場合、古いキャッシュを一度削除する必要があります。

sudo yum remove -y google-chrome-stable
sudo yum clean all
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?