Showing
1 changed file
with
111 additions
and
0 deletions
naver_jido.ipynb
0 → 100644
1 | +{ | ||
2 | + "cells": [ | ||
3 | + { | ||
4 | + "cell_type": "code", | ||
5 | + "execution_count": null, | ||
6 | + "id": "1321df8d", | ||
7 | + "metadata": {}, | ||
8 | + "outputs": [], | ||
9 | + "source": [ | ||
10 | + "pip install selenium" | ||
11 | + ] | ||
12 | + }, | ||
13 | + { | ||
14 | + "cell_type": "code", | ||
15 | + "execution_count": null, | ||
16 | + "id": "a164fe86", | ||
17 | + "metadata": {}, | ||
18 | + "outputs": [], | ||
19 | + "source": [ | ||
20 | + "pip install webdriver-manager" | ||
21 | + ] | ||
22 | + }, | ||
23 | + { | ||
24 | + "cell_type": "code", | ||
25 | + "execution_count": null, | ||
26 | + "id": "9b4d72ec", | ||
27 | + "metadata": {}, | ||
28 | + "outputs": [], | ||
29 | + "source": [ | ||
30 | + "import requests \n", | ||
31 | + "from bs4 import BeautifulSoup\n", | ||
32 | + "from selenium import webdriver\n", | ||
33 | + "from selenium.webdriver.common.keys import Keys\n", | ||
34 | + "from selenium.webdriver.common.by import By\n", | ||
35 | + "from selenium.webdriver.support.ui import WebDriverWait\n", | ||
36 | + "from selenium.webdriver.support import expected_conditions as EC\n", | ||
37 | + "from time import sleep\n", | ||
38 | + "from webdriver_manager.chrome import ChromeDriverManager\n", | ||
39 | + "\n", | ||
40 | + "url = 'https://map.naver.com/v5/search'\n", | ||
41 | + "driver = webdriver.Chrome(ChromeDriverManager().install())\n", | ||
42 | + "driver.get(url)\n", | ||
43 | + "key_word = '영통역식당' \n", | ||
44 | + "\n", | ||
45 | + "# CSS_SELECTOR를 찾을 때까지 일정 시간 대기\n", | ||
46 | + "def time_wait(num, code):\n", | ||
47 | + " try:\n", | ||
48 | + " wait = WebDriverWait(driver, num).until(\n", | ||
49 | + " EC.presence_of_element_located((By.CSS_SELECTOR, code)))\n", | ||
50 | + " except:\n", | ||
51 | + " print(code, '태그를 찾지 못하였습니다.')\n", | ||
52 | + " driver.quit()\n", | ||
53 | + " return wait\n", | ||
54 | + "\n", | ||
55 | + "time_wait(10,'input.input_search') # 10초 대기\n", | ||
56 | + "\n", | ||
57 | + "search = driver.find_element(By.CSS_SELECTOR, 'input.input_search') # 검색창 찾기\n", | ||
58 | + "search.send_keys(key_word) # 검색어 입력\n", | ||
59 | + "search.send_keys(Keys.ENTER) # 엔터버튼 누르기\n", | ||
60 | + "\n", | ||
61 | + "res = driver.page_source # 페이지 소스 가져오기\n", | ||
62 | + "soup = BeautifulSoup(res, 'html.parser') # html 파싱하여 가져온다\n", | ||
63 | + "sleep(1)\n", | ||
64 | + "\n", | ||
65 | + "def switch_frame(frame):\n", | ||
66 | + " driver.switch_to.default_content() # frame 초기화\n", | ||
67 | + " driver.switch_to.frame(frame) # frame 변경\n", | ||
68 | + " res\n", | ||
69 | + " soup\n", | ||
70 | + "\n", | ||
71 | + "def page_down(num):\n", | ||
72 | + " body = driver.find_element(By.CSS_SELECTOR, 'body')\n", | ||
73 | + " body.click()\n", | ||
74 | + " for i in range(num):\n", | ||
75 | + " body.send_keys(Keys.PAGE_DOWN)\n", | ||
76 | + "\n", | ||
77 | + "switch_frame('searchIframe') # frame 변경\n", | ||
78 | + "page_down(100) # 스크롤 다운\n", | ||
79 | + "sleep(5)\n", | ||
80 | + "\n", | ||
81 | + "element_list = driver.find_elements(By.CSS_SELECTOR ,'span.place_bluelink.TYaxT') # element_list에 식당 이름에 해당하는 요소들 저장\n", | ||
82 | + "print(len(element_list)) # 검색된 매장 개수\n", | ||
83 | + "store_list = []\n", | ||
84 | + "for i in range(len(element_list)):\n", | ||
85 | + " store_list.append(element_list[i].text) # text로 바꿔 store_list에 저장\n", | ||
86 | + "print(store_list)" | ||
87 | + ] | ||
88 | + } | ||
89 | + ], | ||
90 | + "metadata": { | ||
91 | + "kernelspec": { | ||
92 | + "display_name": "Python 3 (ipykernel)", | ||
93 | + "language": "python", | ||
94 | + "name": "python3" | ||
95 | + }, | ||
96 | + "language_info": { | ||
97 | + "codemirror_mode": { | ||
98 | + "name": "ipython", | ||
99 | + "version": 3 | ||
100 | + }, | ||
101 | + "file_extension": ".py", | ||
102 | + "mimetype": "text/x-python", | ||
103 | + "name": "python", | ||
104 | + "nbconvert_exporter": "python", | ||
105 | + "pygments_lexer": "ipython3", | ||
106 | + "version": "3.9.7" | ||
107 | + } | ||
108 | + }, | ||
109 | + "nbformat": 4, | ||
110 | + "nbformat_minor": 5 | ||
111 | +} |
-
Please register or login to post a comment