[ERROR] Base - Something went wrong: 'latin-1' codec can't encode characters~
한글이 들어간 어플 이름 크롤링 시도 시 에러가 뜨는 현상
app-store-scraper
Single API ☝ App Store Review Scraper 🧹
pypi.org
# 홈페이지에서 가져온 기본 작동 코드
from app_store_scraper import AppStore
from pprint import pprint
minecraft = AppStore(country="nz", app_name="앱 이름")
minecraft.review(how_many=20)
pprint(minecraft.reviews)
pprint(minecraft.reviews_count)
파이썬으로 어플 리뷰 분석 프로젝트를 진행하려 app-store-scraper를 이용하던 중에 다음과 같은 에러가 발생했다.
한글이 들어간 이름의 어플이었다.
# 출력 결과
2025-01-05 17:23:12,717 [INFO] Base - Searching for app id
2025-01-05 17:23:13,896 [INFO] Base - Initialised: AppStore('kr', '앱이름', '앱아이디')
2025-01-05 17:23:13,897 [INFO] Base - Ready to fetch reviews from: https://apps.apple.com/kr/app/앱이름/앱아이디
2025-01-05 17:23:13,968 [ERROR] Base - Something went wrong: 'latin-1' codec can't encode characters in position 30-35: ordinal not in range(256)
2025-01-05 17:23:13,969 [INFO] Base - [id:1248716281] Fetched 0 reviews (0 fetched in total)
[]
0
4번째 줄에서 latin-1 인코딩 오류가 발생하고 있음을 알 수 있다.
영어로 되어있는 어플 이름도 검색해봤더니,
from app_store_scraper import AppStore
from pprint import pprint
tistory = AppStore(country="kr", app_name="google-chrome")
tistory.review(how_many=3)
pprint(tistory.reviews)
pprint(tistory.reviews_count)
# 출력 결과
2025-01-05 19:05:14,450 [INFO] Base - Searching for app id
2025-01-05 19:05:15,816 [INFO] Base - Initialised: AppStore('kr', 'google-chrome', 535886823)
2025-01-05 19:05:15,817 [INFO] Base - Ready to fetch reviews from: https://apps.apple.com/kr/app/google-chrome/id535886823
2025-01-05 19:05:16,150 [INFO] Base - [id:535886823] Fetched 20 reviews (20 fetched in total)
영어로 되어있는 어플 이름은 에러메세지도 안뜨고 문제 없이 크롤링에 성공했다(크롤링 내용은 올리지 않았음).
한글이 문제라고 생각해, 인코딩을 해주는 수정 작업을 거쳤다.
해결 방법
첫번째 방법 :
먼저 한글이 들어간 어플 이름을 euc-kr, utf-8로 인코딩해주고
# 어플 이름 입력
text = "한글이 들어간 어플 이름"
# euc-kr, utf-8로 인코딩
encoded_euc_kr = text.encode("euc-kr")
encoded_utf_8 = text.encode("utf-8")
인코딩 된 값을 출력하고 복사하여 하드코딩식으로 직접 넣어주거나
from app_store_scraper import AppStore
from pprint import pprint
app = AppStore(country="kr", app_name="인코딩 된 어플 이름")
app.review(how_many=3)
pprint(app.reviews)
pprint(app.reviews_count)
두번째 방법 :
첫번째 방법과 같이 한글로 들어간 어플 이름을 euc-kr, utf-8로 인코딩해주고
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 [encoded_euc_kr, encoded_utf_8]:
try:
app = AppStore(country="kr", app_name=str(encoded_name), app_id=어플 아이디)
app.review(how_many=3)
if app.reviews:
print(f"Success with encoding: {encoded_name}")
pprint(app.reviews)
pprint(app.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.")
for 문을 이용해서 euc-kr, utf-8 둘 다 시도하는 이유는 어떤 어플들은 euc-kr이 아닌 utf-8 형식으로 인코딩 된 값만 검색이 되기 때문이다.
또, 인코딩 한 값(encoded_euc_kr, encoded_utf_8)은 데이터타입이 바이트인데 app_name에는 문자열이 들어가야 하므로 str()을 써줬다.
아쉬운 점은 어플 아이디를 하드코딩해야 한다는 점인데 search_id 를 이용하여 어플 아이디를 찾을 수도 있지만 그렇게 하면 쌩뚱맞게 전혀 다른 어플의 리뷰를 긁어와서... 원하는 결과를 얻기위해 아직은 하드 코딩으로 어플 아이디를 넣어주고 있다. 이 부분에 대해서는 더 연구해서 수정해보고자 한다!
* 아래 글에 수정 방법을 올렸다.
2025.01.06 - [Python/공부] - [app-store-scraper] 한글 이름 어플의 app_id 검색 오류와 원하는 어플의 app_id 정확히 찾는 방법
'Python > 오류' 카테고리의 다른 글
| [error]AttributeError: 'super' object has no attribute '__sklearn_tags__' (0) | 2025.02.04 |
|---|---|
| [hanspell] 'result' 오류 없이 코랩에서 실행하기(2025년 실행 ok) (1) | 2025.01.27 |