플그래밍/파이써언

[파이썬] 009. 네이버금융 '많이 본 뉴스'

훗티v 2020. 12. 15. 21:00

네이버금융 '많이 본 뉴스'

 

 

많이 본 뉴스 제목 및 기사 내용

 

 

코드

from bs4 import BeautifulSoup
import requests
import re
import os

URL = 'https://finance.naver.com/news/news_list.nhn?mode=RANK&page=1'

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 OPR/67.0.3575.115'}

page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser', from_encoding="utf8")
article_content = soup.find("div", class_="hotNewsList")
article_content_links = article_content.findAll("a")

# print(article_content_links)

for i in article_content_links:
    print(f'< {i.get_text()} >')
    x = i.attrs["href"]
    # print(x)

    URL = 'https://finance.naver.com/' + x

    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 OPR/67.0.3575.115'}

    page = requests.get(URL, headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser', from_encoding="utf8")
    article = soup.find("div", class_="articleCont")
    article_full = article.get_text().strip()
    article_brief = article_full.split(". ", 1)[:-1]
    for x in article_brief:
        print(x + ". ", end="")
    print('\n')

 

 

 

 

 

728x90