LoginSignup
2
1

More than 5 years have passed since last update.

Qiita の投稿からスニペットを取り出す

Last updated at Posted at 2018-04-12

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

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
1