본문 바로가기
728x90

파이썬49

문자열안에 변수 값넣는 방법 - 파이썬 문자열 안에 변수를 넣고 싶을 경우 문자열 " 앞에 f 를 붙이고 문자열안에 변수를 {} 로 감싸주면 변수 값이 문자열안으로 들어간다 temp1 = "test" temp2 = f"This is {temp1}" print(temp2) --> This is test 2024. 7. 23.
파일저장하기 - 파이썬 filepath = "test.txt" #파일 경로를 지정하여 파일열기with open(filepath, "w") as file: file.write("test input\n")​#파일 내용 추가하기with open(filepath, "a") as file: file.write("test input\n")​#인코딩 오류 시 인코딩해서 저장하기with open(filepath, "a", encoding='utf-8') as file: file.write("test input\n") 2024. 7. 18.
태그 삭제하기 - 파이썬 temp 문자열 안에있는 태그를 삭제하는 소스​tagsFormat = re.compile("")temp = re.sub(tagsFormat, "", temp) 2024. 7. 18.
selenium 사용하기 - 파이썬 from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsimport subprocessfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import By​subprocess.Popen(r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\chromeCookie"')​o.. 2024. 7. 18.
cloudflare 우회해서 크롤링 하기 - selenium, 파이썬 이전 글에서 zenrows 를 이용해 cloudflare 를 우회해서 크롤링하는 소스를 올렸는데​테스트를 하다보니 잘되는데 프리 이용횟수를 초과하니 막혀버려서 다른 방법을 찾게 되었다. ​아래 방법은 selenium 을 이용하는 방법으로 물론 무료이다. ​원래 selenium 을 이용할 경우 cloudflare가 bot 으로 인식해서 크롤링이 막히는데​해당 방법은 bot 으로 인식하지 않도록 처리했다. ​몇일 테스트를 해봤는데 문제없이 잘 돌아간다. ​from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsimport subprocessfrom webdriver_manager.chrome import Chrome.. 2024. 7. 18.
한번에 여러개 문자열 치환기 - 파이썬 한번에 하나씩 치환하는 것이 아닌 지정된 모든 문자열을 한번에​치환해 주는 함수이다. ​replacements 에 치환을 원하는 문자들을 정리해주고 함수를 호출하면​한번에 치환해서 반환해준다.​ import re def mreplace(text, replacements):    def rmatch(match): return replacements[match.group(0).lower()]        regex = re.compile("|".join(map(re.escape, replacements.keys())), re.IGNORECASE)    return regex.sub(rmatch, text) #치환 문자열 replacements = {    "[메일]": "[Mail]",    "[속보]": .. 2024. 7. 18.