티스토리 뷰

이번엔 프로그래머스에서 문제를 풀게 되었다.

 

역시나 문제를 풀고 나서 유의미한 문제들은 포스팅을 한다.

 

하지만 페이지의 형태가 바뀌었지 않는가.

 

기존 코드에서 수정해서 태그 추출기를 또 만들었다

 

코드

import requests
import pyperclip
from bs4 import BeautifulSoup

def get_text_from_link(link):
    a = link.split('/')[-1]
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
    }
    response = requests.get(link, headers=headers)
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')
    title_element = soup.find('title')
    text = title_element.string if title_element else None
    if text:
        parts = text.split(' - ')
        if len(parts) >= 2:
            text = parts[-1].split(' | ')[0]  # '-' 이후부터 '|' 전까지 추출
            text = text.replace("'", "")  # 작은 따옴표(') 제거
    return a, text

# 링크 입력 받기
link = input("링크를 입력하세요: ")

# 함수 호출 및 결과 출력
a, b = get_text_from_link(link)

output = "코딩테스트, 코딩테스트 준비, 프로그래머스, 프로그래머스 {}, 프로그래머스 {} 파이썬, {} 파이썬, ys.k, samron3".format(b, b, b)

print(output)

pyperclip.copy(output)

얼마나 쓰겠냐만은, 그래도 필요한 사람이 있으면 해당 코드가 한 사람에게라도 도움이 될 수 있기를.

 

챗gpt 만세!

댓글