본문 바로가기
Python

[네이버] 추천검색어 크롤링3

by 퍼포먼스마케팅코더 2017. 1. 18.
반응형

#네이버 검색어 URL 만들기


from bs4 import BeautifulSoup

import urllib.parse

import urllib.request


file = open("C:/Users/eyeden-FF14/Desktop/Auction/all_keywords.txt", 'r' )

Food_keyword_list = open("C:/Users/eyeden-FF14/Desktop/Food_keyword_list.txt", 'w' , encoding='utf-8' )


keywords = file.readlines()

file.close()


Naver_first_URL = 'https://m.search.naver.com/search.naver?query='

Naver_behind_URL = '&where=m&sm=mtp_hty'


for i in keywords :

      a = i.replace("\n", "")

      b = str(a)

      c = urllib.parse.quote(b)   #한글 키워드를 utf-8로 변환

      URL = Naver_first_URL + c +Naver_behind_URL

      Food_keyword_list.write(URL + '\n')

Food_keyword_list.close()


#네이버키워드 추천검색어 뽑기


import urllib.request

import urllib.parse

from bs4 import BeautifulSoup


file = open("C:/Users/eyeden-FF14/Desktop/Food_keyword_list.txt", 'r' , encoding='utf-8')

outcome = open("C:/Users/eyeden-FF14/Desktop/Food_recommended_keyword_list.txt", 'w', encoding='utf-8')

urls = file.readlines()


for page in urls[0:260] :

      url = page.replace("\n", "")

      f = urllib.request.urlopen(url)

      html = f.read()

      bs = BeautifulSoup(html, 'html.parser')

      keywords = bs.find_all("a", class_="basic_show")

      for i in keywords :

            outcome.write(str(i) + '\n')

outcome.close()






반응형

댓글