naver_jido.ipynb 3.33 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1321df8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install selenium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a164fe86",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install webdriver-manager"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b4d72ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests \n",
    "from bs4 import BeautifulSoup\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from time import sleep\n",
    "from webdriver_manager.chrome import ChromeDriverManager\n",
    "\n",
    "url = 'https://map.naver.com/v5/search'\n",
    "driver = webdriver.Chrome(ChromeDriverManager().install())\n",
    "driver.get(url)\n",
    "key_word = '영통역식당'  \n",
    "\n",
    "# CSS_SELECTOR를 찾을 때까지 일정 시간 대기\n",
    "def time_wait(num, code):\n",
    "    try:\n",
    "        wait = WebDriverWait(driver, num).until(\n",
    "            EC.presence_of_element_located((By.CSS_SELECTOR, code)))\n",
    "    except:\n",
    "        print(code, '태그를 찾지 못하였습니다.')\n",
    "        driver.quit()\n",
    "    return wait\n",
    "\n",
    "time_wait(10,'input.input_search') # 10초 대기\n",
    "\n",
    "search = driver.find_element(By.CSS_SELECTOR, 'input.input_search') # 검색창 찾기\n",
    "search.send_keys(key_word)  # 검색어 입력\n",
    "search.send_keys(Keys.ENTER) # 엔터버튼 누르기\n",
    "\n",
    "res = driver.page_source  # 페이지 소스 가져오기\n",
    "soup = BeautifulSoup(res, 'html.parser')  # html 파싱하여  가져온다\n",
    "sleep(1)\n",
    "\n",
    "def switch_frame(frame):\n",
    "    driver.switch_to.default_content()  # frame 초기화\n",
    "    driver.switch_to.frame(frame)  # frame 변경\n",
    "    res\n",
    "    soup\n",
    "\n",
    "def page_down(num):\n",
    "    body = driver.find_element(By.CSS_SELECTOR, 'body')\n",
    "    body.click()\n",
    "    for i in range(num):\n",
    "        body.send_keys(Keys.PAGE_DOWN)\n",
    "\n",
    "switch_frame('searchIframe') # frame 변경\n",
    "page_down(100) # 스크롤 다운\n",
    "sleep(5)\n",
    "\n",
    "element_list = driver.find_elements(By.CSS_SELECTOR ,'span.place_bluelink.TYaxT') # element_list에 식당 이름에 해당하는 요소들 저장\n",
    "print(len(element_list)) # 검색된 매장 개수\n",
    "store_list = []\n",
    "for i in range(len(element_list)):\n",
    "    store_list.append(element_list[i].text) # text로 바꿔 store_list에 저장\n",
    "print(store_list)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}