因此,我尝试从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}'发布于 2019-02-24 09:51:01
这里有一个小技巧--基本上是一行。由于在JSON中通常忽略换行符和其他空格,因此您可以利用这一点,将response转换为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"}发布于 2019-02-24 09:05:12
此调用:
str((fetchProxy()))将转义字符\n括起来。您想要调用替换为
proxy = str((fetchProxy())).replace("\\n","")或者,如果您希望将字符串值而不是字节保存在文件中:
proxy = fetchProxy().decode().replace('\n', '')发布于 2019-02-24 09:05:59
您必须这样做,其中.replace("\n", "")是一个变量(我正在将您的第一个函数更改为一个变量,以便在第二个函数中可用,我希望这不会影响想要的结果):
fetchProxy= return urllib.request.urlopen("https://api.getproxylist.com/proxy").read()
fetchProxy=str(fetchProxy)
fetchProxy=fetchProxy.replace("\n", "")
def createList():
afile = open("proxies.json", "a")
i = 1
for i in range(3):
proxy = fetchProxy
#str((fetchProxy())).replace("\n","",-1)
#I am not sure why the last argument in the .replace is -1, because the last argument is number of
#occurences to be replaced and default is all, if left empty
afile.write(proxy + "\n")
afile.close()https://stackoverflow.com/questions/54847676
复制相似问题