Python/공부

[app-store-scraper] 한글 이름 어플의 app_id 검색 오류와 원하는 어플의 app_id 정확히 찾는 방법

maango97 2025. 1. 6. 02:17

2025.01.05 - [Python/오류] - [ERROR] app-store-scraper 실행 시 Base - Something went wrong: 'latin-1' codec can't encode characters~

 

[ERROR] app-store-scraper 실행 시 Base - Something went wrong: 'latin-1' codec can't encode characters~

[ERROR] Base - Something went wrong: 'latin-1' codec can't encode characters~한글이 들어간 어플 이름 크롤링 시도 시 에러가 뜨는 현상   app-store-scraperSingle API ☝ App Store Review Scraper 🧹pypi.org# 홈페이지에서 가

maango97.tistory.com

 

 

지난 글에서 app-store-scraper를 사용하던 중에 발생한 인코딩 관련 문제에 대해서 다뤄봤다.

당장 인코딩 에러가 나는 코드를 수정하는데에는 성공 하였으나 app_id를 하나씩 하드코딩해서 넣어줘야 한다는 점(왜인지 라이브러리 내부 검색 함수를 이용하면 쌩뚱맞은 어플의 id를 가져온다)이 아쉬웠는데 해결 방법을 알아냈다.

 

 

app_id는 앱스토어에서 해당 앱 페이지의 url을 보면 알 수 있다. 티스토리의 app_id는 906304982이다.

 

해결 방법

 

우선, base.py에 들어있는 search_id 함수 수정해야 한다.

# 원래 base.py의 search_id 함수
def search_id(self):
	search_url = "https://www.google.com/search" # <-- 구글에서 검색
    self._get(search_url, params={"q": f"app store {self.app_name}"})
    pattern = fr"{self._base_landing_url}/[a-z]{{2}}/.+?/id([0-9]+)"
    app_id = re.search(pattern, self._response.text).group(1)
    return app_id

 

 

보면 app.name을 구글에서 검색하고 있음을 알 수 있다.

이를 아래 코드처럼 앱스토어 홈페이지에서 직접 검색하도록 함수를 수정해준다.

 

# 수정 코드
def search_id(self):
    # 앱 이름을 URL 인코딩
    app_name_encoded = quote(self.app_name.replace(" ", "-"))
    app_store_url = f"https://apps.apple.com/{self.country}/app/{app_name_encoded}"

    # HTTP GET 요청
    self._get(app_store_url)

    # HTML에서 앱 ID 추출
    pattern = r"id([0-9]+)"
    match = re.search(pattern, self._response.text)
    if match:
        app_id = match.group(1)
        return app_id
    else:
        raise ValueError("App ID not found in the App Store page.")

 

오류 원인

한글이 들어간 어플의 app_name을 넣어주려 인코딩을 했더니 인코딩 된 값이 구글에서 검색이 되어버려서 엉뚱한 어플의 리뷰들을 가져왔던 것이다!

 

 

최종 코드

결론적으로 아래가 최종 코드이다.

from app_store_scraper import AppStore
from pprint import pprint

# 어플 이름 입력 
text = "한글이 들어간 어플 이름"

# euc-kr and utf-8으로 인코딩
encoded_euc_kr = text.encode("euc-kr")
encoded_utf_8 = text.encode("utf-8")

# 크롤링
success = False
for encoded_name in [text, encoded_utf_8, encoded_euc_kr]:
    try:
        stock = AppStore(country="kr", app_name=str(text))
        app_id = stock.search_id()
        stock = AppStore(country="kr", app_name=str(encoded_name), app_id=app_id)
        stock.review(how_many=3)
        if stock.reviews:
            print(f"Success with encoding: {encoded_name}")
            pprint(stock.reviews)
            pprint(stock.reviews_count)
            success = True
            break
    except Exception as e:
        print(f"Error with encoding: {encoded_name}, Error: {e}")

if not success:
    print("Failed with all encodings.")

 

app_id를 가져올때는 텍스트 그대로 사용하고, 크롤링할 리뷰를 가져올 때는 인코딩된 어플 이름을 사용하도록 코드를 수정하였다.

또한, 데이터타입을 문자열로 확실히 넣기 위해 str()함수도 넣어줬다.

 

 

 

이제 앱스토어에서 app_id를 하나씩 복붙할 필요 없이 한글이 들어간 어플 이름도 app-store-scraper 라이브러리 내부 검색 기능을 통해

app_id를 검색할 수 있게 됐다!

'Python > 공부' 카테고리의 다른 글

[ML] SHAP 분석 - 어떤 변수가 중요한가?  (0) 2025.01.18