简体   繁体   English

api 端点上的发布请求不返回数据

[英]Post request on api endpoint does not return data

I'm currently trying to use the API and the headers of a website instead of creating a web scraper.我目前正在尝试使用 API 和网站的标题,而不是创建 web 刮板。 I have been trying to inspect the website by using Chrome Dev Tools and capturing the information under the network tab when an POST request has been made.我一直在尝试使用 Chrome 开发工具检查网站,并在发出 POST 请求时捕获network选项卡下的信息。

So far I have no luck and have only been getting <Response [200]> from the endpoint but no data returned.到目前为止,我没有运气,只从端点获得<Response [200]>但没有返回数据。

Please advise what I did wrong.请指教我做错了什么。

Below is my Python code:下面是我的 Python 代码:

    import requests

    def get_data_from_api():
    search_keyword = 'money'
    api_endpoint = 'https://45bwzj1sgc-dsn.algolia.net/1/indexes/*/queries?x-algolia-agent=Algolia%20for%20JavaScript%20(4.2.0)%3B%20Browser'
    payload = {
        'x-xxxx-application-id': '45BWZJ1SGC', 
        'x-xxx-api-key': 'MTRkMGNlOGFhMWQ4YThmZDA0YmViNGY2M2ViYzBlMGMwNTBmMjllNjJmMDA4YmY1YzY3YzI2NzRmMzlhYThkM3RhZ0ZpbHRlcnM9JTVCJTVCJTIyc3VzX3B1YmxpYyUyMiUyQyUyMnN1c19jaGFubmVsX2FsbCUyMiUyQyUyMnN1c19jaGFubmVsX2ZlYXR1cmVkJTIyJTJDJTIyc3VzX2NoYW5uZWxfd2Vla2x5LW5ld3NsZXR0ZXIlMjIlMkMlMjJzdXNfY2hhbm5lbF9hbWElMjIlMkMlMjJzdXNfY2hhbm5lbF9saXZlc3RyZWFtJTIyJTJDJTIyc3VzX2NoYW5uZWxfc2hvdy1zdXMlMjIlMkMlMjJzdXNfY2hhbm5lbF9lbmdpbmVlcmluZyUyMiU1RCU1RCZhbmFseXRpY3NUYWdzPSU1QiUyMnN1cyUyMiU1RA=='
        }

    r = requests.post(api_endpoint, headers= payload, data=str(''' {"requests":[{"indexName":"OmniSearch_sus_production","query":"'''+search_keyword+'''","params":"attributesToSnippet=%5B%22body%3A80%22%2C%22searchable_comments%3A30%22%5D&page=0&hitsPerPage=10&filters=&facetFilters=%5B%5B%5D%5D&facets=%5B%22channel%22%5D&sortFacetValuesBy=count&clickAnalytics=true"}]} ''')) 

    print(r)

if __name__ == "__main__":
    get_data_from_api()

You can have a try:你可以试一试:

print(r.json())

the r is an response object. r响应object。 You can use您可以使用

  • r.content get the binary body. r.content获取二进制正文。
  • r.text get the string body r.text获取字符串正文
  • r.json() or orjson.loads(r.content) get the json body r.json()orjson.loads(r.content)得到 json 主体

Check out more details by here在此处查看更多详细信息

To follow up on my comment, there are a few places where you can let Python do the hard work for you.为了跟进我的评论,您可以在几个地方让 Python 为您完成艰苦的工作。 Consider this alternative that uses urllib.parse.urlencode to create the params value, then lets Requests serialize your data structure:考虑使用urllib.parse.urlencode来创建params值的替代方案,然后让 Requests 序列化您的数据结构:

from urllib.parse import urlencode

import requests


def get_data_from_api():
    search_keyword = "money"
    api_endpoint = "..."
    headers = {
        "x-xxxx-application-id": "...",
        "x-xxx-api-key": "...",
    }

    params = {
        "attributesToSnippet": '["body:80","searchable_comments:30"]',
        "page": "0",
        "hitsPerPage": "10",
        "facetFilters": "[[]]",
        "filters": "",
        "facets": '["channel"]',
        "sortFacetValuesBy": "count",
        "clickAnalytics": "true",
    }

    r = requests.post(
        api_endpoint,
        headers=headers,
        data={
            "requests": [
                {
                    "indexName": "OmniSearch_sus_production",
                    "query": search_keyword,
                    "params": urlencode(params),
                }
            ]
        },
    )

    return r.json()

Quick, ApplePie?快点,苹果派? It's 2AM and something broke and everyone's in a panic!现在是凌晨 2 点,有些东西坏了,每个人都惊慌失措! Which version of the code (which is functionally identical) would you rather have to troubleshoot?您宁愿对哪个版本的代码(功能相同)进行故障排除?

I know this doesn't directly answer your question, but take this in the spirit of "while we're on the subject...".我知道这并不能直接回答您的问题,而是本着“当我们讨论这个主题时......”的精神来看待这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有已发送表单数据的 POST 请求未返回正确的响应 - POST request with sent form data does not return correct response 如何添加 http 标头以及数据以使用 Oauth2Session (requests_oauthlib) 将 POST 请求发送到 API 端点? - How to add http headers along with data to send a POST request to an API endpoint using Oauth2Session (requests_oauthlib)? 发布请求不会返回适当的数据 - Post request won't return appropriate data 对 TRIAS API 的 POST 请求不适用于请求 - POST request to TRIAS API does not work with requests 向 Flask API 发送 Post 请求,但未接收数据 - Sending a Post request to Flask API, but not receiving data Flask 端点 - 处理请求中的数据还是端点本身? - Flask endpoint - Handling the data in the request or endpoint itself? FastAPI 端点未接收发布数据 - FastAPI endpoint not receiving post data 通过 POST 请求返回列表 - Return a list by a POST Request Django - 用于更新多个表的 POST 端点。 返回包含所有相关数据的响应 - Django - POST endpoint to update multiple tables. Return response with all relevant data 签名不匹配 - 使用 Python 向 BingX API 发布 HTTP 请求 - Signature does not match - POST HTTP Request to BingX API with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM