Skip to content

Latest commit

 

History

History
180 lines (127 loc) · 6.12 KB

2_scraping.md

File metadata and controls

180 lines (127 loc) · 6.12 KB

2.スクレイピング

スクレイピングとは Web サイトから自分のほしい情報を取り出して収集する技術のことをいいます。
おおまかな処理の流れとしては以下のイメージです。

scraping-imagine

Python で簡単にスクレイピングを行う際には以下のサードパーティー製のライブラリを使うと便利です。

処理 ライブラリ ドキュメント
Web ページから情報を取得 (図の1,2) Requests Requests: HTTP for Humans
response からほしい情報を取り出す (図の3) Beautiful Soup 4 Beautiful Soup Documentation
  • Beautiful Soup 4 は有志による日本語翻訳版のドキュメントもあります。 ただし 2013-11-19 で更新が止まっているので注意。 参考までに。

requests - Webからデータを取得する

PythonでWebへのアクセスをする時に最も手軽な方法は requests を使う方法です。pipでインストールできます。 GETとPOSTはrequests.getとrequests.postを使えば大体事足ります。

インストール

$ pip install requests

詳しくはこちらを参照してください。 http://requests-docs-ja.readthedocs.org/en/latest/

BeautifulSoup4 - HTMLを解析する

HTMLを解析するにはBeautifulSoup4を使うと良いでしょう。

インストール

$ pip install beautifulsoup4
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<div><h1 id="test">TEST</h1></div>', 'html.parser')
>>> soup.select_one('div h1#test').text
'TEST'

タグ内の文字はsoup.textで、属性にはsoup['id'] (idのところは属性名)でアクセスできます。

BeautifulSoup objectのよく使うmethod

  • BeautifulSoup.find() -> タグを検索して最初にhitしたタグを返す
  • BeautifulSoup.find_all() -> タグを検索してhitしたタグのリストを返す
  • BeautifulSoup.find_previous() -> 一つ前のタグを返す
  • BeautifulSoup.find_next() -> 一つ後ろのタグを返す
  • BeautifulSoup.find_parent() -> 親タグを返す
  • BeautifulSoup.select() -> css selectorでタグのリストを返す
  • BeautifulSoup.select_one() -> css selectorで検索して最初にhitしたタグを返す

詳しくはこちらを参照してください。 https://www.crummy.com/software/BeautifulSoup/bs4/doc/

データの永続化

CSV形式

CSVはカンマで区切られた形式のファイルです。csvモジュールが使えます。 csv モジュールのさらに詳しい内容についてはこちらを参照してください。 http://docs.python.org/ja/3.6/library/csv.html

書き込み

import csv
with open('some.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(['1', '2', '3'])

読み取り

import csv
with open('some.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

JSON形式

JSON形式もよく使われる形式です。標準モジュールの json モジュールを使用します。

>>> import json
>>> json.dumps([1, 2, 3, 4])
'[1, 2, 3, 4]'
>>> json.loads('[1, 2, 3, 4]')
[1, 2, 3, 4]
>>> json.dumps({'aho': 1, 'ajo': 2})
'{"aho": 1, "aro": 2}'
>>> json.loads('{"aho": 1, "ajo": 2}')
{'aho': 1, 'ajo': 2}
  • json.dumps() -> オブジェクトをJSON文字列にする
  • json.loads() -> JSON文字列をオブジェクトにする
  • json.dump() -> オブジェクトをJSON文字列にしてそれをファイルに書き込む
  • json.load() -> ファイル内のJSON文字列を読み込んでオブジェクトにする

詳しい内容についてはこちらを参照してください。 http://docs.python.org/ja/3.6/library/json.html

サンプル

PyNyumon #8 の講師・メンターを取得する

Python入門者向けハンズオン #8 - connpass の講師・メンター一覧を取得するものです。

import requests
from bs4 import BeautifulSoup


def main():

    # connpass の PyNumon#8 のイベント参加者・申込者一覧のURL
    url = 'https://python-nyumon.connpass.com/event/100817/participation'

    # requests で参加者一覧の情報と取得する
    response = requests.get(url)

    # response から HTML 部分(content) を取得
    content = response.content

    # BeautifulSoup に content を渡して解析の準備をする
    soup = BeautifulSoup(content, 'html.parser')

    # <div class="participation_table_area mb_20">  に該当するものを取り出す
    # participation_tables は List
    participation_tables = soup.find_all('div', class_='participation_table_area mb_20')

    # participation_tables を順番に見て、"講師・メンター枠"の情報を取り出す
    for participation_table in participation_tables:
        # <table><thead><tr><th> に該当するタグの要素を取り出す (参加者枠の種類が記載されているので)
        participant_type = participation_table.table.thead.tr.th.get_text()

        # 参加者枠を示す文字に "講師・メンター枠" が含まれるものを取り出す
        if '講師・メンター枠' in participant_type:
            mentors_table = participation_table
            break

    # 講師・メンター枠の HTML の中で class=display_name に該当するものを取り出す
    # mentor_names は List
    mentor_names = mentors_table.find_all(class_='display_name')

    # 取り出した 講師・メンター枠の要素から純粋な名前だけを取り出す(前後の無駄な空行や改行などを取り除く)
    for mentor_name in mentor_names:
        print(mentor_name.get_text().strip())


if __name__ == '__main__':
    main()

上記の内容を pynyumon8-mentors.py といった適当な名前のファイルに保存して実行すると利用できます。

実行すると以下のとおりになります。

$ python pynyumon8-mentors.py
nikkie
kashew_nuts
shi-ma
Kei Iwasaki
mocamocaland