LoginSignup
0
4

More than 1 year has passed since last update.

【スクレイピング】Webサイト上の画像取得&保存(全自動化)

Last updated at Posted at 2022-10-02

Todo

こちらのサイトから全ての画像を取得し、保存する

前提

JupyterLabのインストールを行っておく(目安:5分)

実施

  1. JupyterLabを起動

    $ jupyter lab
    
  2. コード作成

    全自動画像取得&保存.ipynb
    # import
    import requests
    from bs4 import BeautifulSoup
    from PIL import Image
    import io
    
    url = 'https://scraping-for-beginner.herokuapp.com/image'
    res = requests.get(url)
    
    soup = BeautifulSoup(res.text, 'html.parser')
    
    # 全てのimgタグを取得
    img_tags = soup.find_all('img')
    
    for i, img_tag in enumerate(img_tags):
    
        # 画像のURL = rootのURL + 画像のsrc
        root_url = 'https://scraping-for-beginner.herokuapp.com'
        img_url = root_url + img_tag['src']
    
        # 画像取得
        img = Image.open(io.BytesIO(requests.get(img_url).content))
        
        i += 1
        
        # 一桁の場合は十の位を0で埋める
        if i < 10:
            i = "{0:02d}".format(i)
    
        # 保存
        img.save(f'img/sample{i}.jpg')
    
  3. 実行
    command + Enter

  4. Webサイト上の画像がすべて保存されていることを確認
    スクリーンショット 2022-10-02 16.40.28.png

関連

参考

0
4
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
4