1
2

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.

ブログの画像だけスクレイピングする

Posted at

よくあるやつだと思うけど、ブログの中から画像だけスクレイピングしたい

やりたいけど活用法がそんなない。

活用法を私は考えた

  1. 私の好きすぎる絵日記ブログがついにとうとうアニメ化してしまった
  2. アニメという媒体であれば、子供とも楽しめる(布教)
  3. しかし原作も知る必要もある(偏見)
  4. そうだスクレイピングしよう

ソース概要を説明する

  1. urlは、ブログの新着記事一覧に指定する
  2. for文で指定したページ数のループし、記事ごとのurl収集する
  3. 記事ごとにsoup獲得して(表現として合ってるのか)、imgタグかつclass名がpictを選択する
  4. あとは画像のsrcを取得し、指定フォルダに格納していく

#ソース全文

img_Blog
from bs4 import BeautifulSoup
import urllib.request as req
import urllib
import os
import time
from urllib.parse import urljoin

pageName = "mamekos"

for page in range(1, 3):
    #新着記事一覧ページ
    url = 'https://XXXXXXXXXXX.blog.jp/?p=' + str(page)
    
    res = req.urlopen(url)
    soup = BeautifulSoup(res, "html.parser")
    titles = soup.find_all("a", itemprop="url")
    for title in titles:
        print(title.get_text())
#        print(title.get("href"))
        res = req.urlopen(title.get("href"))
        soup = BeautifulSoup(res, "html.parser")
        result = soup.find_all("img", class_='pict')
#        print(result)
        link_list =[]
        for link in result:
            href = link.get("src")
            link_list.append(href)
            png_list = [temp for temp in link_list if temp.endswith('png')]
#        print(png_list)
        abs_dbpdf_list = []
        for relative in png_list:
            temp_url = urljoin(url, relative)
            abs_dbpdf_list.append(temp_url)
#        print(abs_dbpdf_list)
        filename_list = []
        for target in abs_dbpdf_list:
            temp_list = target.split("/")
            filename_list.append(temp_list[len(temp_list)-1])
#        print(filename_list)
        target_dir = "./" + pageName

        savepath_list = []
        for filename in filename_list:
            savepath_list.append(os.path.join(target_dir, filename))
        #print(savepath_list)

        for (pdflink, savepath) in zip(abs_dbpdf_list, savepath_list):
            urllib.request.urlretrieve(pdflink, savepath)
            time.sleep(2)

url = 'https://XXXXXXXXXXX.blog.jp/?p=' + str(page)の部分に好きなブログのurl情報を貼り付けたらいいと思う。

使用目的や注意事項

ブログ側に負荷がかからない程度に配慮すること、また個人利用を前提とすることと認識しています。
ブラウザのままで子供に見せると、どうしても他の広告記事にリンクしたりしますし、他のアプリを見上がって、布教西性が出るかと思い、この方法にたどり着いた。

作った感想と反省点

最高に楽しかった。
近所の公民館でスクレイピングサークル作りたいくらいだ。
運動会の競技にも、走らず、サンプルソースをもとに、課題のデータを抽出せよ!とか、そういうのがあったらいいのにと思った。
どうしてもブログの絵日記部分だけ抽出したかったが、絵日記部分だけ指定するclassやdivタグを見つけられなかった。

#次の課題

  • ショッピングサイトの画像の取得
  • 近所で見れる野鳥のリストから簡易図鑑のようなものを生成したい
  • スクレイピングをぼちぼち楽しんでいる友だちが欲しい。
1
2
2

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?