我是新手Python开发者。 我有一个PHP索引数组。使用get请求获取数据。
数据:
b'Array\n(\n [0] => HTTP/1.1 301 Moved Permanently\n [1] => Location: http://www.blahblah.com/\n [2] => HTTP/1.1 200 OK\n [3] => Date: Mon, 29 Oct 2018 09:29:10 GMT\n [4] => Server: blahblah\n [5] => Content-Length: 98836\n [6] => Last-Modified: Mon, 29 Oct 2018 08:13:21 UTC\n [7] => device_type: Touch\n [8] => content-language: en\n [9] => Set-Cookie: JSESSIONID=U-i_J9VsfqNazZl3uF4dAj2-7BmV370w58TXINO5UckAel4uGT5O!-115865558; path=/cs; HttpOnly\n [10] => Keep-Alive: timeout=300\n [11] => Connection: Keep-Alive\n [12] => Content-Type: text/html; charset=UTF-8\n [13] => Set-Cookie: NSC_q6.kjp.dpn_dmvtufs_WT.2*80=ffffffff0985006c45525d5f4f58455e445a4a422828;expires=Mon, 29-Oct-2018 09:31:10 GMT;path=/;httponly\n)\n\n'
我想将其转换为JSON对象。
{
"[0]" : "HTTP/1.1 301 Moved Permanently",
"[1]" : "Location: http://www.blahblah.com/",
"[2]" : "HTTP/1.1 200 OK"
"[3]" : "Date: Mon, 29 Oct 2018 09:29:10 GMT",
......,
.....,
我尝试过加载JSON的代码:
import requests
import json
u1="http://blahblah.com"
req=requests.get("http://gen.demotest.com/httpsProxy/httpsgetheaders.php?url=%s"%(u1))
data=req.content
print(json.dumps(data))
但是遇到了一些错误:
TypeError: Object of type bytes is not JSON serializable
Array\n...
垃圾信息;PHP应该发布已编码的表单或JSON数据。(并且还要注意,你想要的输出不是JSON格式,而是PHP;JSON看起来像{"0": "HTTP...", ...}
或可能更好地作为数组["HTTP...", ...]
)。 - Daniel Roseman