Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@ekzemplaro

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

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
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?