[英]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())
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.