LoginSignup
2
2

More than 3 years have passed since last update.

Qiita:Teamの記事をダウンロードして Markdown + 画像 にする

Posted at

はじめに…

ふだんQiita:Team使ってます(使わせていただいております)が、たまーにQiita:Teamが見れない人にその内容を共有する必要があります。そのたびにコピペとかも疲れるので記事をまるっとダウンロードできるスクリプトをPythonで書きました。

コード

それほど説明する必要もないかと思いますがコード内の ★★★ の箇所はご自身の環境にあわせて書き換えてください。
item_id はQiita:Teamの記事のURLの最後の部分のハッシュみたいな文字列です(https://qiita.com/567000/items/dde495d6a8ad1c25fa43dde495d6a8ad1c25fa43の部分)。

URLはご自身の環境のURL。 Bearer はご自身のアカウントでトークンを発行してください。

Pythonを実行すると、itemid のフォルダが出来てその中に .md と 画像一式 が格納されますのでそこから先はお好きなように!! そのまま渡しても良いですし、PDFにしてもよいですし。

itemid = '★★★'

import urllib.request
import json
import re
import os

## 格納先フォルダ作成
dirname = 'qiita_' + itemid
os.makedirs(dirname, exist_ok=True)

## Qiita:Team
url = 'https://★★★.qiita.com/api/v2/items/' + itemid
headers = {
    'Content-type': 'application/json',
    'Authorization': 'Bearer ★★★'
}
req = urllib.request.Request(url,headers=headers)
with urllib.request.urlopen(req) as res:
    body_str = res.read()
    body = json.loads(body_str)
    body = body["body"]

## 画像のパスを書き換えて md を保存
re_body = re.sub(r'!\[.*\]\(.*/(.*?)\)', r'![](\1)', body, flags=re.MULTILINE)
with open( dirname + '/' + itemid + '.md', 'w', encoding='utf-8') as f:
    f.write(re_body)

# 画像ファイルをダウンロードする
images = re.findall(r'!\[.*\]\((.*?)\)', body)
for image in images:
    image_url = image
    image_file = re.sub(r'.*/(.*?)', r'\1', image, flags=re.MULTILINE)

    req = urllib.request.Request(image_url,headers=headers)
    with urllib.request.urlopen(req) as res:
        with open(dirname + '/' + image_file, mode="wb") as f:
            f.write(res.read())

補足

md + 画像 の組み合わせしか対応していません。他に何かファイルが埋めこまれているとうまくいかないと思いますので……ご自身でご対応くださいませ。

2
2
1

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
2
2