이번 글에서는 파이썬을 사용해 웹사이트에서 링크 목록을 수집해오는 예제에 대해 소개하려 한다. 간단히 얘기하면 BeautifulSoup 모듈을 사용해 웹사이트를 받아와 특정 태그에 해당하는 항목들을 수집해오는 예제이다. 샘플 코드 예제에서 사용한 웹사이트는 https://example.com/ 으로 실제로 사이트에 들어가면 아래와 같은 화면이 나온다. 샘플 코드는 이 웹사이트의 제목과 링크(More information...)가 바라보는 사이트의 주소를 가져오는 기능을 수행한다. import requests from bs4 import BeautifulSoup url = 'https://example.com' response = requests.get(url) soup = BeautifulSoup(res..
문장을 입력받아 가장 긴 단어를 찾는 예제를 작성해 보았다. 샘플코드 def find_longest_word(sentence): words = sentence.split() return max(words, key=len) # Example usage text = "It only takes about 20 minutes to find out about your accurate English level" print("Longest word:", find_longest_word(text)) 결과 Longest word: accurate
개발을 하다보면 입력값 등의 데이터가 제대로 된 형태인지 검증이 필요한 경우가 있다. 이번 글에서는 pattern을 사용해 데이터를 검증하는 예제를 소개한다. 예제 코드 import re def validate_email(email): pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$' if re.match(pattern, email): return True else: return False # Example usage email_address = input("Enter an email address: ") is_valid = validate_email(email_address) if is_valid: print("Valid email address") else: print("Invali..
datetime 라이브러리를 사용해 현재 날짜와 시간을 받아오는 방법을 소개한다. datetime 라이브러리 날짜와 시간을 조작하는 다양한 기능을 제공하는 라이브러리. 자세한 설명은 아래 링크에서 확인. https://docs.python.org/ko/3/library/datetime.html datetime — Basic date and time types Source code: Lib/datetime.py The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient att..