82 lines
3.7 KiB
Python
82 lines
3.7 KiB
Python
import time
|
|
import json
|
|
import re
|
|
import argparse
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
# 명령줄 인자 설정
|
|
parser = argparse.ArgumentParser(description="상품 정보 필터링 및 구매 링크 변환")
|
|
parser.add_argument('--sources', nargs='+', default=['淘宝', 'tmall', '1688'], help="필터링할 출처 (예: 타오바오 tmall 1688)")
|
|
args = parser.parse_args()
|
|
filtered_sources = set(args.sources) # 선택된 출처를 집합으로 사용
|
|
|
|
# Playwright 사용
|
|
with sync_playwright() as p:
|
|
# 브라우저 실행
|
|
browser = p.chromium.launch(headless=False)
|
|
page = browser.new_page()
|
|
|
|
# 웹사이트 접속
|
|
url = "https://graph.baidu.com/s?card_key=&entrance=GENERAL&extUiData%5BisLogoShow%5D=1&f=all&isLogoShow=1&session_id=&sign=126316b6a2b0c27d34c5c01731123409&tpl_from=pc"
|
|
page.goto(url)
|
|
|
|
# 이미지 업로드
|
|
page.click('//*[@id="app"]/div/div[1]/div/div[1]/div/div/div[1]/span[1]/span[1]')
|
|
page.set_input_files('//*[@id="app"]/div/div[1]/div/div[1]/div/div/div[1]/div/div[2]/div[2]/div/form/input', "1.png")
|
|
time.sleep(5) # 결과 로딩 시간 대기
|
|
|
|
# 검색 결과 페이지에서 JSON 데이터 추출
|
|
content = page.content()
|
|
soup = BeautifulSoup(content, 'html.parser')
|
|
|
|
# JSON 데이터가 포함된 스크립트 태그 추출
|
|
script_tag = soup.select_one("html > head > script:nth-of-type(2)")
|
|
if script_tag:
|
|
# 불필요한 부분 제거
|
|
raw_data = script_tag.string.strip()
|
|
match = re.search(r"window\.cardData\s*=\s*(\[\{.*\}\]);", raw_data, re.DOTALL)
|
|
if match:
|
|
json_data_str = match.group(1) # JSON 데이터만 추출
|
|
|
|
try:
|
|
# JSON으로 변환
|
|
data = json.loads(json_data_str)
|
|
|
|
# 필요한 데이터 추출 및 출력
|
|
for card in data:
|
|
if card.get("cardName") == "product":
|
|
products = card["tplData"]["list"]
|
|
for product in products:
|
|
title = product.get("desc", "")
|
|
source = product.get("source", "")
|
|
price = product.get("text", "")
|
|
buyurl = product.get("buyurl", "")
|
|
|
|
# 필터링할 출처 체크
|
|
if source in filtered_sources:
|
|
# 원래 페이지 링크 요청
|
|
try:
|
|
response = requests.get(buyurl, headers={
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
|
|
})
|
|
# 리디렉션된 URL 가져오기
|
|
original_url = response.url if response.status_code == 200 else "변환 실패"
|
|
except requests.RequestException:
|
|
original_url = "변환 실패"
|
|
|
|
# 결과 출력
|
|
print(f"상품명: {title}, 출처: {source}, 가격: {price}")
|
|
print(f"구매 링크 (암호화): {buyurl}")
|
|
print(f"구매 링크 (원래 링크): {original_url}\n")
|
|
except json.JSONDecodeError as e:
|
|
print("JSON 디코딩 오류:", e)
|
|
else:
|
|
print("JSON 데이터를 찾을 수 없습니다.")
|
|
else:
|
|
print("JSON 데이터가 포함된 스크립트를 찾지 못했습니다.")
|
|
|
|
# 브라우저 종료
|
|
browser.close()
|