-
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 : 만약 ~가 아니면 이후에 따라올 실행문을 실행시켜라
A.startswith("This is string") : A 문자열의 시작이 "This is string" 으로 시작하는지 확인하는 메소드
** f "문자열 {변수명}"
-> string 문자열 내부에 변수를 넣는 방법
3 _ 웹 사이트 응답 받기
: requests 모듈의 get 명령어를 사용해서 각 웹 사이트들의 반응을 확인할 수 있다.
- get(website) 를 통해서 website들의 http 반응이 성공적임을 확인했다. ( response 200 : 정상 )
- 최종적으로 results 라는 dictionary을 만들어서 website를 key, 연결 결과를 값으로 구성했다.
** if elif 문으로 오류코드 200말고 다른 부분도 OK 나 FAILED 로 지정할 수 있다.
ex) 200번대는 어떤 종류 / 300번대는 어떤 종류 / 400번대는 오류 등
'Python' 카테고리의 다른 글
데이터 타입(자료형 검사) (0) 2023.03.18 input 자료형 검증 ( try - excpt ) (0) 2023.03.16 nomard) python기초 ( ERROR 모음 ) (0) 2023.03.10 nomard) python기초 (Flask로 웹페이지 만들기) (0) 2023.03.10 nomard) python 기초 (1) (0) 2023.03.06