2016년 9월 29일 목요일

Windows 환경에서 Python Selenium 설치

precodition : python3.5 설치 및 path 설정

(1) python selenium 라이브러리 설치


C:\PYTHON_HOME\..\Scripts
pip install -U selenium
실행 시 SSL 오류가 발생하였음

 Could not fetch URL https://pypi.python.org/simple/selenium/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) - skipping
  Could not find a version that satisfies the requirement selenium (from versions: )
No matching distribution found for selenium

[해결방안]

  1. pip upgrade(pip 버전 확인 pip --version)
  2. easy_install pyopenssl 실행
  3. pip install --upgrade --trusted-host pypi.python.org selenium








출처 : http://selenium-python.readthedocs.io/installation.html


Python if __name__ == "__main__" 정리, 설명

if __name__ == "__main__" 의미
파이썬에서 일반적으로 import 방식으로 다른 코드를 불러오는데, 파이썬이 스크립트 언어이기에 그때마다 불려온 파일에 있는 메소드와 클래스를 실행 시킨다면 원하는대로 사용하기가 힘들기에

if __name__ == "__main__" 구문을 통해 실행될 코드와 그냥 참조 대기상태로 존재할 코드를 구분할 수 있다.
__name__ 은 보통 해당 파일의 위치+이름으로 구성돼있다.
from django import db의 경우 db.__name__을 찍어보면 'django.db'가 나온다.

test.py

def say_hi():

    print("Hi")



if __name__=="__main__":

    say_hi()
test.py 파일을 python test.py 로 실행한다면 say_hi() 메소드가 실행된다. 직접 실행되는 파일의 __name__은 __main__으로 설정된다.
그러나 다른 파일에서 import test 로 위 파일을 불러온다면 say_hi()는 실행되지 않는다. 기본적으로 test.py의 __name__ 은 그냥 'test'이다. (파일명)