# ---
# 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
# ---

# +
# Appannie 용 스크래퍼
# -

# 배운 라이브러리
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import pandas as pd
import csv
import time

# 찾아본 라이브러리
import pyautogui as gui # 마우스 이동, 키보드 입력
import pyperclip # 클립보드 사용

browser = webdriver.Chrome('C:\chromedriver.exe')

browser.get('https://www.appannie.com/intelligence/top-apps/store-rank/ios?date=%272021-07-19%27&country_code=KR&device_code=ios-phone&category_id=100000&store_rank_ios_chart_free$previous_range$chart_compare_facets=!(store_product_rank_grossing__aggr)&store-rank.ios.view=free&store_rank.ios.chart_range=7')

# 스크래퍼 작동을 위해 화면이 top에 위치 & 페이지크기 full
browser.maximize_window()
browser.implicitly_wait(10)

html= browser.page_source

browser.execute_script("window.scrollTo(0, 950)")

# +
contents = []

# 1 ~ 91 순위까지
for i in range(1, 92):
    gui.moveTo(490,236,1)
    gui.dragTo(1173, 280, 1,button='left')
    gui.hotkey('ctrl','c')
    a = pyperclip.paste()
    gui.click()
    gui.scroll(-60)
    list = a.split('\r')
    list = [i.strip('\n') for i in list]
    time.sleep(0.6)
    if len(list) == 3:
        list.insert(2, '없음')
        contents.append(list)
        continue
    elif len(list) == 4:
        contents.append(list)
        continue
#     else:
#         break
# -

# 92 ~ 100 순위 까지 (마지막은 스크롤을 내릴 수 없어서 따로 for문 작성)
for i in range(1,10):
    n = 75 * (i-1)
    gui.moveTo(490,236+int(n),1)
    gui.dragTo(1173, 280+int(n), 1,button='left')
    gui.hotkey('ctrl','c')
    a = pyperclip.paste()
    gui.click()
    list = a.split('\r')
    list = [i.strip('\n') for i in list]
    time.sleep(0.5)
    if len(list) == 3:
        list.insert(2, '없음')
        contents.append(list)
        continue
    elif len(list) == 4:
        contents.append(list)
        continue

len(contents)

contents

# 드래그시 열의 마지막 str에서 긴 (Application) 의 경우 1~2문자의 누락가능성 확인 -> 조정하기위한 for문
for content in contents:
    if content[-1][-1] == 'n':
        content[-1] = content[-1] + 's)'
    elif content[-1][-1] == 'o':
        content[-1] = content[-1] + 'ns)'
    elif (content[-1] == 'Games') or (content[-1] == 'Kids'):
        break
    elif content[-1][-1] == 's':
        content[-1] = content[-1] + ')'

pd.set_option('display.max_row', 101)
pd.set_option('display.max_columns', 10)

df = pd.DataFrame(data = contents, columns = ['-', '앱이름','회사명','카테고리'])
df.index = df.index + 1
df = df.drop(columns = ['-'])
df

# * Games : Action, Adventure, Board, Card, Casino, Casual, Dice, Education, Family, Kids, Music, Puzzle, Racing, Role Playing, Simulation, Sports, Strategy, Trivia, Word
#
# * Applications : Books, Business, Catalogs, Developer Tools, Education, Entertainment, Finance, Food and Drink, Graphics & Design, Health and Fitness, Lifestyle, Magazines and Newspapers, Medical, Music, Navigation, News, Photo and Video, Productivity, Reference, Shopping, Social Networking, Sports, Travel, Utilities, Weather
#
# * Kids : Ages 5 & Under, Ages 6-8, Ages 9-11

# +
# 회사명 中 None 은 '없음' 으로 본다
# -

df.to_excel('C:\pl/contents.xls')

StarcoderdataJupyterScriptsDedupFiltered