Python
-
User-Agent 지정 ( header )Python 2023. 3. 20. 10:58
header 지정 : 크롤링을 할 때 해당 페이지에서 로봇인지 사람인지 검사하기위해 header (유저) 정보를 요구할 때가 있다. 다음의 경우 User-Agent를 지정해서 이를 해결할 수 있다. User-Agent 지정 페이지를 가져오기 위해 get 함수를 다음과 같이 사용한다. get( URL , headers = {'User-Agent' : 유저정보} ) 따라서 보통 headers = {' ' : } URL get( USL , headers = headers ) 의 형태로 사용할 수 있다 유저정보 얻는 곳 https://www.useragentstring.com/ UserAgentString.com - unknown version www.useragentstring.com - 각 브라우저에 맞춤형..
-
데이터 타입(자료형 검사)Python 2023. 3. 18. 23:56
int와 string이 뒤석여있는 배열을 for 문을 활용해서 int 값들만 합을 구하는 문제가 있었다. numbers = [ int와 string이 뒤 섞인 값들 ] sum_result = 0 for num in numbers: if isinstance(num,int): sum_result = sum_result + num print(sum_result) - 다음과 같이 for 문 내부에 if문으로 배열의 값이 int인지 아닌지 검사하는 명령어를 넣어서 해결했다. python에서 타입을 확인하는 방법 두가지가 있다 type 과 isinstance 이다. 둘의 차이점으로 type은 해당 데이터의 type을 반환하고 isinstance는 true/false로 반환한다 type(데이터) -> 데이터의 타입을..
-
input 자료형 검증 ( try - excpt )Python 2023. 3. 16. 10:54
숫자 두개를 입력받아서 + , - , * , / 할 수 있는 코드를 작성했다. 숫자를 2번 input에 입력하고, operation을 입력하면 해당 식의 결과를 출력해 준다. operation을 입력할 때 잘 못 된 값이 입력되면 Type correct information을 출력하고 while문을 다시 시작하도록 만들었다. 하지만, 숫자를 입력할 때 숫자가 아닌 다른 데이터를 입력하면 오류가 발생하고 프로그램이 멈췄다. 이를 방지하기 위해 try - except 문을 사용해서 input에 잘 못된 자료형이 입력되었을 때 이를 처리할 수 있도록 설정했다. try 문에서 input에 int가 아닌 다른 자료형이 입력될 경우 발생하는 에러 ( ValueError )를 except 문에서 예외처리하고 해당 w..
-
nomard) python기초 ( ERROR 모음 )Python 2023. 3. 10. 15:51
https://nomadcoders.co/python-for-beginners Python으로 웹 스크래퍼 만들기 – 노마드 코더 Nomad Coders Python for Beginners nomadcoders.co 2023.03.10 Selenium headless 적용하기 : selenium으로 crom을 경유해서 특정 페이지에 접근할 경우 한번 접근할 때 마다 browser를 열어야하는 부담이 있다. 이를 해결할 수 있는 코드가 두 개 있다. # --headless : scrapping 할 때 browser를 띄우지 않는다. options.add_argument("--headless") # --disable-gpu : headless 사용시 발생하는 오류를 막아준다. options.add_argu..
-
nomard) python기초 (Flask로 웹페이지 만들기)Python 2023. 3. 10. 11:03
https://nomadcoders.co/python-for-beginners 1) Flask 다운로드, 서버 만들기 vscode의 터미널에서 pip install Flask Flask 다운로드 후 - flask 로 부터 Flask를 import해서 사용할 수 있다. app = Flask("JobScrapper") -> Flask 선언 app.run("0.0.0.0") -> Flask를 실행해서 괄호 속의 ip를 가진 서버를 만든다. ( 0.0.0.0 ) 은 누구나 접근 가능 @app.route("/") def home(): return "hey there!" -> 제일 위의 decorator가 함수를 decorating 하고 있을 때만 동작한다 .route("/") 의 뜻은 도메인 이후에 나오는 / 밑..
-
nomad) python 기초 (2)Python 2023. 3. 6. 12:14
https://nomadcoders.co/python-for-beginners 1 _ For Loops : 기본형태 for placeholder in 반복할요소: 실행문 -> 반복할요소 내부의 인수들을 각각 placeholder를 붙여서 실행문을 실행한다. - websites의 모든 인수들을 불러와서 each에 저장시킨 다음 실행문을 실행한다. ( 모든 인수가 실행문을 실행할 때 까지 ) 2 _ For, If 문 활용 : list 내부의 링크들이 이동 가능한지 확인하기 websites 내부 string들의 시작부분에 https:// 가 있는지 검사해서 없으면 추가해주는 코드 ** if not website.startswith("https://") -> if not : 만약 ~가 아니면 이후에 따라올 실행..
-
nomard) python 기초 (1)Python 2023. 3. 6. 11:12
https://nomadcoders.co/python-for-beginners 1 _ 파이썬 기초 문법 : 기본적으로 print() 같은 명령어는 똑같이 쓰인다. - 정보를 입력 받고 싶을 때 input("사용자에게 하고싶은 말") : input을 이용하면 사용자에게 값을 입력받는 것은 물론 특정한 값을 입력해달라고 명시할 수 있다. input으로 입력받은 값은 string 형태이므로 값이 숫자라면 int(input()) 으로 바꿔서 저장해야한다 변수 = int(input("숫자를 입력해주세요")) 2 _ 조건문 ( if / else / elif ) if 문 if condition: 실행문 else 문 if condition: 실행문 else: 실행문 elif 문 if condition: 실행문 elif..