Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';在向api/url发出请求后是否打印响应?_Python_Api - Fatal编程技术网

Python 为什么可以';在向api/url发出请求后是否打印响应?

Python 为什么可以';在向api/url发出请求后是否打印响应?,python,api,Python,Api,我刚刚通过检查xhr找到了bestbuyCA api aboveurl = 'https://www.bestbuy.ca/ecomm-api/availability/products?accept=application%2Fvnd.bestbuy.simpleproduct.v1%2Bjson&accept-language=en-CA&skus=14962185' 我试过:: response= requests.get(aboveurl) print(res

我刚刚通过检查xhr找到了bestbuyCA api

 aboveurl = 'https://www.bestbuy.ca/ecomm-api/availability/products?accept=application%2Fvnd.bestbuy.simpleproduct.v1%2Bjson&accept-language=en-CA&skus=14962185'
我试过::

  response= requests.get(aboveurl)
  print(response.text)
//


当我在vsc中运行我的代码时,它会启动并保持运行,但不会显示任何内容。

您必须向请求添加一个标题才能获得请求结果:

import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}
aboveurl = "https://www.bestbuy.ca/ecomm-api/availability/products?accept=application%2Fvnd.bestbuy.simpleproduct.v1%2Bjson&accept-language=en-CA&skus=14962185"
html = requests.get(aboveurl,headers=headers)
print(f'requrest code: {html.status_code}\n request text: {html.text}')

只是快速查看一下,您的URL看起来不像URL=''上面的字符串。很抱歉,这是一个输入错误。您可能需要在请求中添加HTTP头。谢谢,有没有我可以阅读的文档可以帮助我理解这样的事情,比如为什么它需要一个标题,以及当我对回复进行查询时api在寻找什么,有没有我可以阅读的推荐文档来解释这一点?是的,你可以阅读官方的感谢,很抱歉再次打扰你。我试图用html.json将“html”转换为json,但它给了我一个错误代码“json.decoder.jsondecoderror:意外的UTF-8 BOM(使用UTF-8-sig解码)”。有没有办法将响应“html”转换为json?“html”是,它不应该包含json吗?嗨,我可以通过使用“decoded_data=html.text.encode().decode('utf-8-sig')data=json.loads(decoded_data)”来解决它,这很好^^
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}
aboveurl = "https://www.bestbuy.ca/ecomm-api/availability/products?accept=application%2Fvnd.bestbuy.simpleproduct.v1%2Bjson&accept-language=en-CA&skus=14962185"
html = requests.get(aboveurl,headers=headers)
print(f'requrest code: {html.status_code}\n request text: {html.text}')