AutoTao/1688/api_1688.py

132 lines
4.6 KiB
Python

# import requests
# import base64
# def upload_image(image_path):
# # 이미지 파일을 Base64로 인코딩
# with open(image_path, "rb") as image_file:
# encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
# base64_image = f"data:image/jpeg;base64,{encoded_image}" # JPEG 이미지 예제
# # 요청 URL
# url = "https://api.itemscout.io/api/crossborder/1688/image"
# # 요청 헤더
# headers = {
# "Accept": "*/*",
# "Accept-Encoding": "gzip, deflate, br, zstd",
# "Accept-Language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,zh;q=0.5",
# "Cache-Control": "no-cache",
# "Content-Type": "application/json",
# "Origin": "https://itemscout.io",
# "Pragma": "no-cache",
# "Referer": "https://itemscout.io/",
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Whale/3.28.266.14 Safari/537.36",
# }
# # 요청 페이로드
# payload = {
# "image": base64_image
# }
# # POST 요청 전송
# response = requests.post(url, json=payload, headers=headers)
# # 응답 처리
# if response.status_code == 200:
# print("요청 성공:", response.json())
# else:
# print(f"요청 실패: {response.status_code}, {response.text}")
# # 사용 예제
# image_path = "test1.jpg" # 업로드할 이미지 파일 경로
# upload_image(image_path)
import requests
import gzip
import struct
import json
import os
def analyze_response_content(response):
try:
# 서버로부터 응답 받기
# response = requests.get(url)
raw_data = response.content
print("응답 데이터 길이:", len(raw_data))
# Content-Type 확인
content_type = response.headers.get("Content-Type", "unknown")
print("Content-Type:", content_type)
# 1. GZIP 해제 시도
try:
decoded_data = gzip.decompress(raw_data).decode('utf-8')
print("GZIP 해제 후 데이터:")
print(decoded_data)
except (OSError, UnicodeDecodeError):
print("GZIP 해제 실패 또는 데이터가 압축되지 않음.")
# 2. JSON 데이터 확인
try:
json_data = json.loads(raw_data.decode('utf-8'))
print("JSON 데이터:")
print(json.dumps(json_data, indent=4, ensure_ascii=False))
except (UnicodeDecodeError, json.JSONDecodeError):
print("JSON 디코딩 실패.")
# 3. 바이너리 데이터 분석
try:
print("바이너리 데이터 분석 시도:")
if len(raw_data) >= 4:
header = struct.unpack("!I", raw_data[:4]) # 첫 4바이트 읽기
print("헤더 데이터:", header)
payload = raw_data[4:]
print("페이로드 데이터 길이:", len(payload))
else:
print("바이너리 데이터가 너무 짧아 분석할 수 없습니다.")
except struct.error as e:
print(f"바이너리 데이터 분석 실패: {e}")
# 4. 다른 문자셋 디코딩
try:
latin1_decoded = raw_data.decode('latin-1')
print("Latin-1(ISO-8859-1) 디코딩 결과:")
print(latin1_decoded[:200]) # 일부만 출력
except UnicodeDecodeError:
print("Latin-1 디코딩 실패.")
# 5. 응답 데이터를 파일로 저장
dump_file = "response_dump.bin"
with open(dump_file, "wb") as file:
file.write(raw_data)
print(f"응답 데이터를 '{dump_file}' 파일로 저장했습니다.")
except Exception as e:
print(f"오류 발생: {e}")
def get_image_result(image_key):
url = f"https://itemscout.io/sourcing/1688/search"
params = {
"imageKey": image_key,
"_rsc": "1ftz3" # 기본 값 사용
}
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "ko-KR,ko;q=0.9,en-US;q=0.8",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Whale/3.28.266.14 Safari/537.36",
"Referer": "https://itemscout.io/sourcing/1688",
}
response = requests.get(url, params=params, headers=headers)
analyze_response_content(response)
# 테스트
image_key = "0b0290d784ebff078490646317f90a23409924bdb08cc4bf8b171819a42f9f37" # 이전 응답 키
get_image_result(image_key)