You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从指定JSON数据中抓取评论?

Extracting Comments from Trendyol's Review JSON Response

Hey there! It looks like you've already nailed the first part—successfully fetching the JSON response from Trendyol's review endpoint. The tricky piece here is that the hydrateScript value you're accessing isn't a ready-to-use JSON object yet; it's a stringified JSON string that needs an extra parsing step to unlock the actual comments. Let's walk through how to get this done:

  • First, pull the hydrateScript string from your initial parsed JSON response.
  • Use Python's json.loads() method to convert this string into a workable dictionary.
  • Navigate through the parsed data to find the comments array (Trendyol typically stores this under a predictable path).

Here's the full, updated code with these steps included:

import requests
import json

headers = {
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Referer': 'https://www.trendyol.com/',
    'Connection': 'keep-alive',
}

params = (
    ('boutiqueId', '594432'),
    ('merchantId', '968'),
    ('culture', 'tr-TR'),
    ('storefrontId', '1'),
    ('logged-in', 'false'),
    ('userId', '0'),
    ('isBuyer', 'false')
)

response = requests.get(
    'https://public-mdc.trendyol.com/discovery-web-socialgw-service/reviews/denokids/yilbasi-kokos-elbise-p-3893218/yorumlar',
    headers=headers,
    params=params
)

result_json = response.json()

# Extract the stringified JSON from hydrateScript
hydrate_script_str = result_json['result']['hydrateScript']

# Convert the string into a usable JSON object
hydrate_data = json.loads(hydrate_script_str)

# Access the comments array (standard path for Trendyol's review data)
comments = hydrate_data['reviewList']['comments']

# Loop through comments to extract key details
for comment in comments:
    print(f"Reviewer: {comment['userFullName']}")
    print(f"Rating: {comment['rating']}/5")
    print(f"Comment Text: {comment['comment']}\n")

A quick breakdown of the new steps:

  • json.loads(hydrate_script_str) turns the raw string into a dictionary you can navigate.
  • Trendyol structures its review data so comments live under reviewList -> comments. Each comment object includes fields like userFullName, rating, and comment (the actual review text).
  • If you want to explore all available data points, add print(json.dumps(hydrate_data, indent=2)) after parsing—it’ll pretty-print the entire structure so you can see what else is available.

内容的提问来源于stack exchange,提问作者Abdurrahman AY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:27:41