Last active
          July 23, 2018 13:05 
        
      - 
      
 - 
        
Save pypeach/0cce105d0e12490f01e68fccc6336269 to your computer and use it in GitHub Desktop.  
    Webスクレイピングを行うサンプルアプリケーションです
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # 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() | 
親クラスを追加した
logger.exceptionを追加
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
URLのデフォルト指定時のNone判定は不要になったため削除した