네이버 기사 가져오기

조회수 548회

https://m.search.naver.com/search.naver?where=m_news&query=1%EB%B3%B4&sm=mtb_tnw&sort=1

위 링크에서 노출되는 기사의 제목을 제일 최근 것부터 시작해 20개의 기사 제목들을 가져오려면 경로 지정을 어떻게 해줘야 하나요??

api_txt_lines tit 까지는 찾았는데 그 다음을 어떻게 해야 할지 모르겠습니다.

  • class로 api_txt_lines tit 까지 찾았으면 그게 끝인걸로 보이는데, 그 다음 내부 텍스트를 가져오면 되지 않을까요? 편집요청빌런 2020.2.18 17:44

1 답변

  • 좋아요

    2

    싫어요
    채택 취소하기

    BeautifulSoup를 활용하여 쉽게 구현할 수 있습니다.

    속보 기사 제목을 출력하는 파이썬 코드입니다.

    from bs4 import BeautifulSoup
    import urllib.request
    
    # html 소스 가져오기
    url = 'https://m.search.naver.com/search.naver?where=m_news&query=1%EB%B3%B4&sm=mtb_tnw&sort=1'
    with urllib.request.urlopen(url) as response:
        html = response.read()
        soup = BeautifulSoup(html, 'html.parser')
    
    articles = soup.find_all('div', {'class': 'api_txt_lines tit'})  # 기사 제목 가져오기
    
    # 기사 제목 출력하기
    for article in articles:
        title = article.text
        print(title)
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)