Python请求HTTP响应406 [英] Python Requests HTTP Response 406

查看:608
本文介绍了Python请求HTTP响应406的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自己的域,用于存储json文件(http://example.com/file.json).使用直接链接在浏览器中访问文件时,json会返回的很好.但是,当在下面的python代码中使用相同的方法时, http响应为 406 .有什么想法吗?

I have my own domain where an json file is stored (http://example.com/file.json). When accesing the file in browser using the direct link, the json is returned just fine. But when using the same approach in my python code below, the http response is 406. Any ideas why?

import requests
url = 'http://example.com/file.json'
r = requests.get(url, headers={"Accept":"text/html"})
print(r.status_code)
print(r.headers)

打印:

406
{'Server': 'nginx/1.14.1', 'Date': 'Sun, 12 May 2019 16:53:25 GMT', 'Content-Type': 'text/html; charset=iso-8859-1', 'Content-Length': '226', 'Connection': 'k
eep-alive'}

推荐答案

通过使用其他User-Agent解决.默认的Python User-Agent 'python-requests/2.21.0'可能已被托管公司阻止.

Solved by using a different User-Agent. The default Python User-Agent 'python-requests/2.21.0' was being probably blocked by the hosting company.

r = requests.get(url, headers={"User-Agent": "XY"})

某些可能的代理:用户代理字符串列表

这篇关于Python请求HTTP响应406的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆