【发布时间】:2015-04-22 18:02:10
【问题描述】:
我们使用 Bugsplatsoftware.com 收集所有崩溃。他们有一个返回 JSON 数据的RESTFull web 服务。我想获取individual crashes 的数据。数据在登录名/密码后面...
我尝试了以下方法,但结果不如预期。
import requests
from requests.auth import HTTPBasicAuth
args={'id':11111,'data':0}
response=requests.get("https://www.bugsplatsoftware.com/individualCrash",params=args,auth=HTTPBasicAuth("username","password"))
data=response.json()
response.headers 返回关注
response.headers
{'content-length': '10549', 'connection': 'close', 'server': 'Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.5.21', 'x-frame-options': 'SAMEORIGIN', 'x-pingback': 'https://www.bugsplatsoftware.com/xmlrpc.php', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'x-xss-protection': '1; mode=block', 'date': 'Wed, 22 Apr 2015 17:43:37 GMT', 'content-encoding': 'gzip', 'link': '<https://www.bugsplatsoftware.com/?p=363>; rel=shortlink', 'vary': 'Accept-Encoding,User-Agent', 'x-content-type-options': 'nosniff', 'x-powered-by': 'PHP/5.5.21', 'content-type': 'text/html; charset=UTF-8', 'pragma': 'no-cache'}
我需要做什么来获取 json 数据?提前致谢。
当我打印 response.url 时,它显示 https://www.bugsplatsoftware.com/login/ 而不是 https://www.bugsplatsoftware.com/individualCrash/?id=11111&data=0....
marmeladze, "bugsplatsoftware.com/individualCrash?id=11111&data=0";返回 json 数据(至少在浏览器中),这就是我需要的。
pygeek,当我调用 response.content 时,数据似乎是 html 页面.....
Ivan,如何为 requests.get 指定“内容类型”?
似乎我需要做类似Using Python Requests: Sessions, Cookies, and POST的事情我尝试了以下
import requests
s=requests.Session()
data={"login":'tester',"password":'testing'}
url="https://wwww.bugsplatsoftware.com/login"
r=s.post(url,data=data)
我收到未经授权的错误消息
或者如果我只是这样做
s.get(url) 我收到了太多重定向
【问题讨论】:
-
您要准确提取什么?
-
您是否尝试过将内容类型从 text/html 更改为 text/json?
-
你检查过响应正文了吗?
标签: python json rest python-requests