【问题标题】:How Can I Scape Comments From This Json Data如何从这个 Json 数据中获取评论
【发布时间】:2022-01-19 15:48:59
【问题描述】:

如何从这个 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()
result_json['result']['hydrateScript']

我不能做更多的事情

【问题讨论】:

    标签: python json web-scraping


    【解决方案1】:

    您可以像这样对 hydraScript 中的文本进行切片,然后使用 json.loads() 获取所需的 JSON 数据:

    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()
    s = result_json['result']['hydrateScript']
    
    start = "window.__REVIEW_APP_INITIAL_STATE__ = "
    end = 'window.TYPageName="product_reviews"'
    dirty = s[s.find(start)+len(start):s.rfind(end)].strip()[:-1] #get the token out the html
    
    review_data = json.loads(dirty)
    
    print(review_data['ratingAndReviewResponse'])
    

    【讨论】:

    • .strip(" \r\n;")?
    • @bushcat69;非常感谢你
    猜你喜欢
    • 2014-03-23
    • 2019-02-18
    • 2021-04-23
    • 2023-03-29
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2013-10-13
    相关资源
    最近更新 更多