python—为什么它不能在向api/url发出请求后打印响应?

busg9geu  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(408)

我刚刚通过检查xhr找到了bestbuycaapi。

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)

//

r = requests.get(url).json()
  print(r)

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

kfgdxczn

kfgdxczn1#

您必须在请求中添加标题才能获得请求结果:

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}')

相关问题