# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# #### 망고플레이트의 api url을 이용하여 특정 검색어의 가게 이름과 하이퍼링크 목록을 나열한다.
# * https://www.mangoplate.com/search/ + 검색어

from bs4 import BeautifulSoup
import requests
import json            #json import하기

custom_header = {
    'referer' : 'https://www.mangoplate.com/',
    'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' }


# +
def get_restaurants(name) :
    # 검색어 name이 들어왔을 때 검색 결과로 나타나는 식당들을 리스트에 담아 반환
    restaurant_list = []
    
    url = 'https://www.mangoplate.com/search/'+name
    req = requests.get(url, headers=custom_header)
    soup = BeautifulSoup(req.text, 'html.parser')
    
    restuarants = soup.find_all('div', class_='list-restaurant-item')
    for rest in restuarants : 
        info = rest.find('div', class_='info')
        href = info.find('a')['href']
        title = info.find('h2').get_text().replace('\n','').replace('  ', '')
        restaurant_list.append([title, href])
        
    return restaurant_list
        

def main() :
    name = input()
    
    restaurant_list = get_restaurants(name)
    
    for rest in restaurant_list:
        # 가게이름 목록 출력
        print(rest[0])
        # 하이퍼링크 목록 출력
        print(rest[1])
        
    
    print(restaurant_list)

if __name__ == "__main__" :
    main()


# -

# ## 음식점 리뷰 크롤링
# * url : https://www.mangoplate.com/restaurants/B8CzA6i9Bb8Z
# * get_reviews : 음식점 리뷰 데이터를 리스트에 담아 반환

# * 리뷰의 시작 인덱스 : start_index, 리뷰의 개수 : request_count 변수의 값으로 조절합니다.
# * start_index 값은 main 함수 내의 i 변수로 조절

# +
def get_reviews(code) :
    comments = []
    
    i = 0
    # https://www.mangoplate.com/restaurants/B8CzA6i9Bb8Z
    while True :
        url = f"https://stage.mangoplate.com/api/v5{code}/reviews.json?language=kor&device_uuid=V3QHS15862342340433605ldDed&device_type=web&start_index={i}&request_count=50&sort_by=2"
        req = requests.get(url, headers = custom_header)
        
        if req.status_code == requests.codes.ok: 
            print("접속 성공")
            reviews = json.loads(req.text)
            
            if len(reviews) == 0:
                break
            
            for review in reviews:
                comment = review['comment']
                # print(comment['comment'])
                text = comment['comment']
                comments.append(text)
                
        else:
            print("Error code")
    
        i = i+50
    return comments
    
    
# restaurants/B8CzA6i9Bb8Z : 가담 식당
def main() :
    href = "/restaurants/B8CzA6i9Bb8Z"
    print(get_reviews(href))
    

if __name__ == "__main__" :
    main()

# +
from bs4 import BeautifulSoup
import requests
import json            #json import하기

custom_header = {
    'referer' : 'https://www.mangoplate.com/',
    'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36' }

def get_reviews(code) :
    comments = []
    
    i = 0
    # https://www.mangoplate.com/restaurants/B8CzA6i9Bb8Z
    while True :
        url = f"https://stage.mangoplate.com/api/v5{code}/reviews.json?language=kor&device_uuid=V3QHS15862342340433605ldDed&device_type=web&start_index={i}&request_count=50&sort_by=2"
        req = requests.get(url, headers = custom_header)
        
        if req.status_code == requests.codes.ok: 
            print("접속 성공")
            reviews = json.loads(req.text)
            
            if len(reviews) == 0:
                break
            
            for review in reviews:
                comment = review['comment']
                # print(comment['comment'])
                text = comment['comment']
                comments.append(text)
                
        else:
            print("Error code")
    
        i = i+50
    return comments
    

def get_restaurants(name) :
    # 검색어 name이 들어왔을 때 검색 결과로 나타나는 식당들을 리스트에 담아 반환하세요.
    restaurant_list = []
    
    url = 'https://www.mangoplate.com/search/'+name
    req = requests.get(url, headers=custom_header)
    soup = BeautifulSoup(req.text, 'html.parser')
    
    restuarants = soup.find_all('div', class_='list-restaurant-item')
    for rest in restuarants : 
        info = rest.find('div', class_='info')
        href = info.find('a')['href']
        title = info.find('h2').get_text().replace('\n','').replace('  ', '')
        restaurant_list.append([title, href])
        
    return restaurant_list
    

def main() :
    name = input("검색어를 입력하세요 : ")
    
    restuarant_list = get_restaurants(name)
    
    for r in restuarant_list :
        print(r[0])
        print(get_reviews(r[1]))
        print("="*30)
        print("\n"*2)

if __name__ == "__main__" :
    main()


StarcoderdataJupyterScriptsDedupFiltered