BeautifulSoup を使って Qiita の投稿からスニペットを取り出します。最初のスニペットだけを取り出します。

get_qiita.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   get_qiita.py
#
#                   Apr/13/2018
#
# ------------------------------------------------------------------
import requests
import sys
from bs4 import BeautifulSoup
#
url = sys.argv[1]
sys.stderr.write("*** 開始 ***\n")
#
try:
    response = requests.get(url=url)
    html=response.text
    try:
        result = BeautifulSoup(html, "html.parser")
#
        span = result.find(class_='code-lang')
        if (hasattr(span,'text')):
            sys.stderr.write(span.text + "\n")
#
        span = result.find(class_='highlight')
        if (hasattr(span,'text')):
            print(span.text)
#
    except Exception as ee:
        sys.stderr.write("*** error *** in BeautifulSoup ***\n")
        sys.stderr.write(str(ee) + "\n")
#

except Exception as ee:
    sys.stderr.write("*** error *** in requests.get ***\n")
    sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行方法

#
URL="https://qiita.com/ekzemplaro/items/01f735d7fd83d160c8dc"
#
./get_qiita.py $URL
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.