gpt4 book ai didi

python - 代码未从 json GET 响应中删除 "\n"

转载 作者:行者123 更新时间:2023-12-01 08:18:28 26 4
gpt4 key购买 nike

所以我试图从 Json GET 请求中删除一堆“\n”。但是,当我制作响应的字符串副本并尝试 .replace("\n", "") 时,没有任何内容被删除

def fetchProxy():
return urllib.request.urlopen("https://api.getproxylist.com/proxy").read()


def createList():
afile = open("proxies.json", "a")

i = 1
for i in range(3):
proxy = str((fetchProxy())).replace("\n","",-1)
afile.write(proxy + "\n")
afile.close()

这是用于获取 json 并将其放入文件中的所有代码(3 次以上)。此外,目前整个项目中几乎所有代码。

示例 json:

b'{\n    "_links": {\n        "_self": "\/proxy",\n        "_parent":
"\/"\n },\n "ip": "177.23.106.107",\n "port": 4145,\n
"protocol": "socks4",\n "anonymity": "high anonymity",\n
"lastTested": "2019-02-23 23:53:48",\n "allowsRefererHeader":
true,\n "allowsUserAgentHeader": true,\n "allowsCustomHeaders":
true,\n "allowsCookies": true,\n "allowsPost": true,\n
"allowsHttps": true,\n "country": "BR",\n "connectTime":
"0.692",\n "downloadSpeed": "155.000",\n "secondsToFirstByte":
"1.128",\n "uptime": "45.621"\n}'

最佳答案

这里有一个小技巧可以做到这一点——基本上是一句俏皮话。由于 JSON 中通常会忽略换行符和其他空格,因此您可以利用这一点,将响应转换为 Python 字典,然后将其转换回 JSON。

import json
import urllib.request


def fetchProxy():
response = urllib.request.urlopen("https://api.getproxylist.com/proxy").read()
return json.dumps(json.loads(response)) # Clean up.

def createList():
proxy = fetchProxy() # Only do it once.

with open("proxies.json", "a") as file:
for _ in range(3):
file.write(proxy + "\n")


if __name__ == '__main__':

createList()

之后的proxies.json内容:

{"_links": {"_self": "/proxy", "_parent": "/"}, "ip": "177.66.42.126", "port": 4145, "protocol": "socks4", "anonymity": "high anonymity", "lastTested": "2019-02-27 12:43:02", "allowsRefererHeader": true, "allowsUserAgentHeader": true, "allowsCustomHeaders": true, "allowsCookies": true, "allowsPost": true, "allowsHttps": true, "country": "BR", "connectTime": "0.773", "downloadSpeed": "135.000", "secondsToFirstByte": "1.281", "uptime": "96.580"}
{"_links": {"_self": "/proxy", "_parent": "/"}, "ip": "177.66.42.126", "port": 4145, "protocol": "socks4", "anonymity": "high anonymity", "lastTested": "2019-02-27 12:43:02", "allowsRefererHeader": true, "allowsUserAgentHeader": true, "allowsCustomHeaders": true, "allowsCookies": true, "allowsPost": true, "allowsHttps": true, "country": "BR", "connectTime": "0.773", "downloadSpeed": "135.000", "secondsToFirstByte": "1.281", "uptime": "96.580"}
{"_links": {"_self": "/proxy", "_parent": "/"}, "ip": "177.66.42.126", "port": 4145, "protocol": "socks4", "anonymity": "high anonymity", "lastTested": "2019-02-27 12:43:02", "allowsRefererHeader": true, "allowsUserAgentHeader": true, "allowsCustomHeaders": true, "allowsCookies": true, "allowsPost": true, "allowsHttps": true, "country": "BR", "connectTime": "0.773", "downloadSpeed": "135.000", "secondsToFirstByte": "1.281", "uptime": "96.580"}

关于python - 代码未从 json GET 响应中删除 "\n",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com