如何从指定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
hydrateScriptstring 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 likeuserFullName,rating, andcomment(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
相关产品推荐
相关产品推荐

