python是否可以将gzip api响应文件拆分为更少的GB

2024-04-19 23:43:09 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,我能够从一个api响应中提取数据,但是文件太大了,超过了4gb,所以我想问一下,是否有办法将gzip文件中的数据分割成更小的块?你知道吗

我试着使用curl命令,我看到数据下载和工作非常好 然后我尝试在python中使用相同的curl逻辑

curl -H "X-Risk-Token: $token" "https://api.nyc3.us.thisismyurl.com/vulnerabilities/download_data_zip" -o file.gz -vv

下面是我的python代码:

import requests
import  gzip
import json
import csv


# url ='https://api.thisismyurl.com/vulnerabilities/download_data_zip'
token = 'blahblahblah'
# 'Content-Type': 'application/json'
headers = {'X-Risk-Token': token, 'Accept': 'application/json'}
response = requests.get(url,headers=headers)
print(response.status_code)
json_format = json.loads(response.text)
print(json_format) 

以下是我的输出: enter image description here 你能举个例子吗?你知道吗

谢谢


Tags: 文件数据httpsimportcomtokenapijson
1条回答
网友
1楼 · 发布于 2024-04-19 23:43:09

要做与curl完全相同的事情,必须执行以下操作:

import requests
import shutil

# curl -H "X-Risk-Token: $token" "https://api.nyc3.us.thisismyurl.com/vulnerabilities/download_data_zip" -o file.gz -vv

url ='https://api.thisismyurl.com/vulnerabilities/download_data_zip'
token = 'blahblahblah'
fname = "file.gz"
# 'Content-Type': 'application/json'
headers = {'X-Risk-Token': token } # exactly same header as curl.

# memory friendly way to download big files
with requests.get(url, headers=headers, stream=True) as resp:
    print(res.status_code)
    with open(fname, 'wb') as fout:
        shutil.copyfileobj(resp.raw, fout)

你知道吗json.loads文件()的原始代码将消耗大量RAM,如果没有足够的可用RAM,可能会导致进程崩溃。你知道吗

你到底想怎么处理这些数据?你知道吗

可视化数据可以通过以下方式完成:

import gzip
import shutil
with gzip.open(fname) as fin:
    shutil.copyfileobj(fin, sys.stdout)

由于我建议仍然没有成功,也没有得到任何信息,这就解释了为什么“配额”会导致python脚本出现问题,而不是curl问题,我建议进行更多的测试。你知道吗

1.)下载但不存储结果(是否是网络被阻止/中止??)你知道吗

url ='https://api.thisismyurl.com/vulnerabilities/download_data_zip'
token = 'blahblahblah'
headers = {'X-Risk-Token': token } # exactly same header as curl.

# memory friendly way to download big files
with requests.get(url, headers=headers, stream=True) as resp:
    print(res.status_code)
    downloaded = 0
    try:
        for chunk in resp.iter_content(chunk_size=1024): 
            downloaded += len(chunk)
    except Exception:
        print("Downloaded %d bytes and got an exception" % downloaded)
        raise
print("Downloaded %d bytes" % downloaded)

请检查结果是否总是相同的,或者您得到的数字是否不同

相关问题 更多 >