Python Can';不要在靓汤中寻找元素

Python Can';不要在靓汤中寻找元素,python,beautifulsoup,Python,Beautifulsoup,我还在学习用python编写代码。我真的需要帮助从这个网站上删除元素: 我想从Review(Ulasan)容器中获取审核数据(审核时间) 这是网站上的HTML <p disabled="" data-testid="txtDateGivenReviewFilter0" class="css-oals0c-unf-heading e1qvo2ff8">1 bulan lalu</p> 或 但结果我只得到了空数

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

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

这是网站上的HTML

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

但结果我只得到了空数据


有人能解决这个问题吗?非常感谢

当您分析网站时,网站会调用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'])
``

所以我必须先获取json数据,然后解析数据?是的,运行脚本,获取json数据,然后解析json。json包含了您要求的所有审阅数据。是否仍然可以使用beautiful soup从find方法获取数据?或者,获取数据的唯一方法是json请求?您无法从beautifulsoup获取数据,因为站点打开后数据不会立即加载。它进行ajax调用,检索数据,然后加载数据。所以,你不能通过beautifulsoup刮。而且这个json包含了所有的审查数据。解析json应该很容易,你能帮我解析我想要的json数据吗?里面有很多数据,我只想得到“时间回顾”数据
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)
print(data[0]['data']['ProductRatingQuery']['ratingScore'])
``