Programming/Python
[Python] 뉴스 사이트 스크랩
goatlab
2022. 11. 24. 20:25
728x90
반응형
SMALL
뉴스 사이트 스크랩
import requests
url = 'https://www.boannews.com/media/t_list.asp'
res = requests.get(url, verify=False)
res.status_code
bs4
from bs4 import BeautifulSoup
bs4obj = BeautifulSoup(res.text, 'html.parser') # html을 파서를 사용해 받아온 뉴스 페이지 분석
bs4obj.title
news_list = bs4obj.find_all('div', {'class':'news_list'})
len(news_list)
테이블 정리
# 제목 뽑기
news_list[0].img.text.strip()
# 해당 기사의 URL 뽑기
news_list[0].a['href']
import pandas as pd
news_dict = dict()
for i, news in enumerate(news_list):
# 제목 뽑기
title = news.span.text.strip()
# 해당 기사의 URL 뽑기
url = news.a['href']
news_dict[i] = [title, 'https://www.boannews.com' + url]
pd.DataFrame(news_dict, index=['title', 'url']).T
728x90
반응형
LIST