Skip to content

Instantly share code, notes, and snippets.

@pypeach
Last active July 23, 2018 13:05
Show Gist options
  • Save pypeach/0cce105d0e12490f01e68fccc6336269 to your computer and use it in GitHub Desktop.
Save pypeach/0cce105d0e12490f01e68fccc6336269 to your computer and use it in GitHub Desktop.
Webスクレイピングを行うサンプルアプリケーションです
# coding:utf-8
import logging
from app.app_logic_base import AppLogicBase
from app.util import message_access, web_access
"""
Webスクレイピングするサンプルアプリケーションです
"""
__author__ = "t.ebinuma"
__version__ = "1.0"
__date__ = "25 February 2018"
class CollectWeb(AppLogicBase):
def __init__(self):
super().__init__()
# loggerを設定する
self.logger = logging.getLogger(__name__)
# デフォルトURL。URL未指定時に使用する
self.access_default_url = "http://mocjax.com/pypeach/pypeach-example/example.html"
def access_url(self, url):
"""
URLにアクセスして<p>タグのテキストを取得する
"""
# アクセスするURLをセットする
if len(url) == 0:
url = self.access_default_url
try:
# <p>のリストを抽出する
p_tag_list = web_access.scrape(url).findAll('p')
for p_tag_item in p_tag_list:
# <p>のテキストを取得する
self.logger.info(p_tag_item.get_text())
except Exception as e:
# エラー発生時はメッセージを出力する
self.logger.exception(message_access.get_message('E901'), url, e)
def main():
app_class = CollectWeb()
app_class.access_url(app_class.access_default_url)
if __name__ == '__main__':
main()
@pypeach
Copy link
Author

pypeach commented May 2, 2018

URLのデフォルト指定時のNone判定は不要になったため削除した

@pypeach
Copy link
Author

pypeach commented May 15, 2018

親クラスを追加した

@pypeach
Copy link
Author

pypeach commented Jul 22, 2018

logger.exceptionを追加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment