找不到有靓汤的元素

2024-10-02 14:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我还在学习用python编写代码。我真的需要帮助从这个网站上删除元素:

https://www.tokopedia.com/craftdale/crossback-apron-hijau-army?src=topads

我想从Review(乌拉桑)容器中获取审核数据(审核时间)

enter image description here

这是网站上的HTML

<p disabled="" data-testid="txtDateGivenReviewFilter0" class="css-oals0c-unf-heading e1qvo2ff8">1 bulan lalu</p>

我已尝试使用此代码获取元素

review = soup.findAll('p',class_='css-oals0c-unf-heading e1qvo2ff8') 

review= soup.findAll('p',id_='txtDateGivenReviewFilter0') 

但结果我只得到了空数据 enter image description here

有人能解决这个问题吗?多谢各位


Tags: 数据代码image元素here网站descriptioncss
1条回答
网友
1楼 · 发布于 2024-10-02 14:16:30

分析网站时,网站会调用ajax来检索网站中的不同信息。为了获得审查信息,它使用json负载对特定端点进行ajax调用

import requests, json

payload = [{"operationName": "PDPReviewRatingQuery", "variables": {"productId": 353506414}, "query": "query PDPReviewRatingQuery($productId: Int!) {\n  ProductRatingQuery(productId: $productId) {\n    ratingScore\n    totalRating\n    totalRatingWithImage\n    detail {\n      rate\n      totalReviews\n      percentage\n      __typename\n    }\n    __typename\n  }\n}\n"}, {"operationName": "PDPReviewImagesQuery", "variables": {"productID": 353506414, "page": 1}, "query": "query PDPReviewImagesQuery($page: Int, $productID: Int!) {\n  ProductReviewImageListQuery(page: $page, productID: $productID) {\n    detail {\n      reviews {\n        reviewer {\n          fullName\n          profilePicture\n          __typename\n        }\n        reviewId\n        message\n        rating\n        updateTime\n        isReportable\n        __typename\n      }\n      images {\n        imageAttachmentID\n        description\n        uriThumbnail\n        uriLarge\n        reviewID\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}, {"operationName": "PDPReviewHelpfulQuery", "variables": {"productID": 353506414}, "query": "query PDPReviewHelpfulQuery($productID: Int!) {\n  ProductMostHelpfulReviewQuery(productId: $productID) {\n    shop {\n      shopId\n      __typename\n    }\n    list {\n      reviewId\n      message\n      productRating\n      reviewCreateTime\n      reviewCreateTimestamp\n      isReportable\n      isAnonymous\n      imageAttachments {\n        attachmentId\n        imageUrl\n        imageThumbnailUrl\n        __typename\n      }\n      user {\n        fullName\n        image\n        url\n        __typename\n      }\n      likeDislike {\n        totalLike\n        likeStatus\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}, {"operationName": "PDPReviewListQuery", "variables": {"page": 1, "rating": 0, "withAttachment": 0, "productID": 353506414, "perPage": 10}, "query": "query PDPReviewListQuery($productID: Int!, $page: Int!, $perPage: Int!, $rating: Int!, $withAttachment: Int!) {\n  ProductReviewListQuery(productId: $productID, page: $page, perPage: $perPage, rating: $rating, withAttachment: $withAttachment) {\n    shop {\n      shopId\n      name\n      image\n      url\n      __typename\n    }\n    list {\n      reviewId\n      message\n      productRating\n      reviewCreateTime\n      reviewCreateTimestamp\n      isReportable\n      isAnonymous\n      imageAttachments {\n        attachmentId\n        imageUrl\n        imageThumbnailUrl\n        __typename\n      }\n      reviewResponse {\n        message\n        createTime\n        __typename\n      }\n      likeDislike {\n        totalLike\n        likeStatus\n        __typename\n      }\n      user {\n        userId\n        fullName\n        image\n        url\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}]

res = requests.post("https://gql.tokopedia.com/", json=payload)

data = res.json()

with open("data.json", "w") as f:
    json.dump(data, f)

上面的脚本将审查信息作为json保存到一个文件中

为了得到评分

print(data[0]['data']['ProductRatingQuery']['ratingScore'])
``

相关问题 更多 >

    热门问题