연습문제와 흥미로운 예제

이해하기 쉽고, 장황하지 않은 자료를 기반으로 강의를 진행합니다.
잔재미코딩 소식 공유
좀더 제약없이, IT 컨텐츠를 공유하고자, 자체 온라인 사이트와, 다음 두 채널도 오픈하였습니다
응원해주시면, 곧 좋은 컨텐츠를 만들어서 공유하겠습니다
●  잔재미코딩 뉴스레터 오픈 [구독해보기]
●  잔재미코딩 유투브 오픈 [구독해보기]

5. 연습문제와 흥미로운 예제

연습문제
1. 다음 뉴스 경제란 뉴스 타이틀 추출하기
- http://media.daum.net/economic/ 로 크롤링된 데이터 중 http:// 로 시작하는 링크를 다시 들어가서, title 태그 정보만 출력해보기

X 참고코드: git 저장소에서 02_examples/crawling_daum_news_title.py 를 참고
- 프로그래밍은 스스로 작성을 해야 합니다. 정 이해하기 어려울 때만 참고코드를 보시면 좋을 것 같습니다.
In [ ]:
import re
import requests
from bs4 import BeautifulSoup

res = requests.get('http://media.daum.net/economic/')
[-------------------------------------------------]

# a태그이면서 href 속성을 갖는 경우 탐색, 리스트 타입으로 links 변수에 저장됨
links = soup.select('a[href]')
for link in links:
    if re.search('http://\w+', link['href']):  # 이 부분도 개선의 여지가 있어보입니다만...(고급)
    [-------------------------------------------------]
    [-------------------------------------------------]
    [-------------------------------------------------]    
연습문제
1. 네이버 실시간검색어 20개 추출하기
- Chrome 의 Copy Selector를 활용

X 참고코드: git 저장소에서 02_examples/crawling_naver_realtime_keyword.py 를 참고
- 프로그래밍은 스스로 작성을 해야 합니다. 정 이해하기 어려울 때만 참고코드를 보시면 좋을 것 같습니다.
In [ ]:
import requests
from bs4 import BeautifulSoup

res = requests.get('https://www.naver.com/')
soup = BeautifulSoup(res.content, 'html.parser')

# a 태그이면서 href 속성 값이 특정한 값을 갖는 경우 탐색
link_title = soup.select([------------------------------------------])
for num in range(len(link_title)):
    [------------------------------------------]
    [------------------------------------------]
흥미로운 예제
1. slack 메세지로 만들어보는 부동산 뉴스
본 자료 보다 업데이트된 자료와 함께 온라인 강의로 익히면 체계적으로 이해할 수 있습니다!
퀄러티가 다른 온라인 강의로 차근차근 익혀보세요
In [46]:
from bs4 import BeautifulSoup
import datetime
import json

random_url = 'https://hooks.slack.com/services/T6NH7FZLG/B74J31P0V'
res = requests.get('http://www.drapt.com/e_sale/index.htm?page_name=esale_news&menu_key=34')
soup = BeautifulSoup(res.content, 'html.parser')

link_titles = soup.find_all('a', class_='c0000000')
link_dates = soup.find_all('span', class_='ffth fs11 c807f7f')
today_date = datetime.date.today()
today_message = '오늘의 부동산 뉴스 [%s]' % str(today_date)
payload = {'text': today_message}
estate_info_message = json.dumps(payload)
requests.post(random_url, data=estate_info_message)

for num, link_title in enumerate(link_titles):
    if str(today_date) == link_dates[num].get_text():
        # estate_info = '<a herf=\"http://www.drapt.com/e_sale/%s\">%s</a>' % (link_title['href'], link_title.get_text())
        # Reference for creating a link: https://api.slack.com/incoming-webhooks
        estate_info = '%d] <http://www.drapt.com/e_sale/%s|%s>' % (num + 1, link_title['href'], link_title.get_text())
        payload = {'text': estate_info}
        estate_info_message = json.dumps(payload)
        requests.post(random_url, data=estate_info_message)