티스토리 뷰
네이버 모바일에서 키워드를 입력하여 검색한 결과의 제목과 링크 정보를 CSV 화일로 저장하는 파이썬 웹 크롤링 연습 예제 입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from bs4 import BeautifulSoup from urllib.request import urlopen from urllib.parse import quote_plus import csv def get_soup(target_url): html = urlopen(target_url).read() soup = BeautifulSoup(html, 'html.parser') return soup def extract_data(soup): total = soup.select('.api_txt_lines.total_tit') searchList = [] for i in total: temp = [] temp.append(i.text) temp.append(i.attrs['href']) searchList.append(temp) f = open(f'{search}.csv', 'w', encoding='utf-8', newline='') csvWriter = csv.writer(f) for i in searchList: csvWriter.writerow(i) f.close() print('완료되었습니다.') search = input('검색어를 입력해주세요:') target_url = f'https://m.search.naver.com/search.naver?where=m_view&sm=mtb_jum&query={quote_plus(search)}' soup = get_soup(target_url) extract_data(soup) | cs |
'개발 > Python 웹 크롤링' 카테고리의 다른 글
| 파이썬 웹 크롤링 연습 예제 #6 (0) | 2020.03.02 |
|---|---|
| 파이썬 웹 크롤링 연습 예제 #4 (0) | 2020.03.02 |
| 파이썬 웹 크롤링 연습 예제 #3 (0) | 2020.03.02 |
| 파이썬 웹 크롤링 연습 예제 #2 (0) | 2020.03.02 |
| 웹 크롤링을 위한 파이썬 설치 및 환경 설정 (0) | 2020.03.02 |
댓글
