네이버금융 '많이 본 뉴스'
많이 본 뉴스 제목 및 기사 내용
코드
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
'플그래밍 > 파이써언' 카테고리의 다른 글
[파이썬 에러] RuntimeError: The current Numpy installation (0) | 2020.12.24 |
---|---|
[파이썬 스크래핑] 서울 날씨 구글에서 긁어오기 (0) | 2020.12.18 |
[파이썬] 008. 유튜브 영상 제목 검색기 (0) | 2020.12.15 |
[파이썬 기초] 너무 쉬운 ZeroDivisionError (0) | 2020.12.14 |
[파이썬 기초] 너무 쉬운 def (0) | 2020.12.11 |