파이썬 네이버 뉴스 키워드 검색

Jmnote (토론 | 기여)님의 2021년 6월 14일 (월) 22:11 판 (→‎개요)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

파이썬 네이버 뉴스 키워드 검색
  • 지정한 키워드로 검색하여 첫번째 기사의 제목과 URL을 추출한다.
import requests
from bs4 import BeautifulSoup

keyword = '코로나'
r = requests.get(f'https://search.naver.com/search.naver?where=news&query={keyword}')
soup = BeautifulSoup(r.text, 'html.parser')
articles = soup.select('ul.list_news > li')
title = articles[0].select_one('a.news_tit')['title']
url = articles[0].select_one('div.info_group > a:nth-of-type(2)')['href'] 
print('title=', title)
print('url=', url)

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}