728x90 파이썬49 GET, POST 사용하기 - Requests 파이썬에서 GET, POST 를 사용하기 위해 Requests를 사용한다. 아래는 사용에 대한 설명을 해봤다. (당연히 설치가 되어 있어야 한다 - pip install requests) * GET 방식import requestsreq = requests.get('https://naver.com')print(req.text) * POST 방식import requestsdata = {'source':'ko', 'target':'target', 'html':'html'}headers = {'X-NCP-APIGW-API-KEY-ID': 'ID', 'X-NCP-APIGW-API-KEY':'KEY'}response = requests.post('https://www.naver.com', headers=head.. 2024. 8. 30. 파이썬에서 url encode, decode 하는 방법 파이썬에서 url encode, decode 하는 방법에 대해서 알아보자 from urllib import parsestr = "hello"str_encode = parse.quote(str)str_decode = parse.unquote(str_encode)print("original : ", str)print("encode : ", str_encode)print("decode : ", str_decode) 위의 샘플 소스를 보면 알겠지만 parse.quote 를 사용하면 encode parse.unquote를 사용하면 decode 를 할 수 있다. 두 함수 모두 파라미터로 safe, encoding, errors 를 사용 할 수 있는데 safe 는 인코딩 하지 않을 값을 encoding 은 인코딩 .. 2024. 8. 29. 문자열 공백제거 함수 - 파이썬 문자열 양 끝에 있는 공백을 제거해주는 함수 //양쪽 공백 제거 temp.strip() //왼쪽 공백 제거 temp.lstrip() //오른쪽 공백 제거 temp.rstrip() 2024. 8. 12. if else 문 한줄에 쓰기 - 파이썬 if else 문을 간단하게 한줄에 쓰고 싶을 경우가 있다. 파이썬은 구조가 좀 특이한데 아래 소스를 확인하면 알 수 있다. if(test==1): print("True")else: print("False") -> 한줄로 요약print("Ture") if(test==1) else print ("False") if(test=="1"): print("1") elif(test=="2"): print("2") else: print("3") -> 한줄로 요약print("1") if(test=="1") else print("2") if(test=="2") else print("3") 2024. 8. 9. 한글 문자열 자르기 - 파이썬 한글 문자열을 원하는 길이만큼 잘라서 배열로 리턴하는 함수를 만들어봤다.def splitString(text, length=10): return [text[i:i+length] for i in range(0, len(text), length)]#사용법temp = "국내 정상급 가수들의 공연·불꽃쇼 등 볼거리 가득 나흘간 카약·요트 등 무료 해상 스포츠 체험 제공 워터슬라이드·에어바운스 갖춘 수상 워터파크 운영"result = splitString(temp, 10)print(result) 2024. 7. 31. 파이썬에서 문자열 이스케이프 처리하기 php 의 addslahses, stripslashes 처럼 이스케이프 처리해주는 함수#문자열을 이스케이프 처리def addslashes(s: str) -> str: return re.sub(r"(['\"\\\0])", r'\\\1', s)#이스케이프된 문자열의 백슬래시를 제거def stripslashes(s: str) -> str: return re.sub(r'\\(.)', r'\1', s) 두번째 방법 ------------------------------------------ # 특수 문자들을 이스케이프합니다.def addslashes(s): return s.replace('\\', '\\\\').replace("'", "\\'").replace('"', '\\"').re.. 2024. 7. 30. 이전 1 2 3 4 5 ··· 9 다음