批量请求时的编码问题?

2024-04-23 06:59:16 发布

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

我正在向api发出请求:

def get_data(text, url='api.com'):
    r = requests.get(url,
                     params={'key': '<My KEY>',
                             'hj': text
                             'oi': 'm'})

    json_data = json.dumps(r.json())
    data = yaml.load(json_data)                
    return data

因为我的数据是在一个数据帧中,所以我应用了如下函数:

    data
0   The quick  fox jumps over the lazy 
1   The quick  fox  over the lazy dog
2   The quick brown fox jumps over the lazy dog
....

n   The  brown fox jumps over the  dog

然后:

df['col'] = df[['data']].apply(get_data, axis=1)

我通过请求发送和接收的数据的大小非常大,因此如何按块发出上述请求?,比如说4乘4?地址:

for chunk in r.iter_content(chunk_size=5):
       json_data = json.dumps(r.json())
       data = yaml.load(json_data)
       return data

不管怎样,有没有人能帮我把请求分成几块,或者分成几块,然后把所有的东西都粘起来?。你知道吗

更新

我还试着将数据帧按块分割,但没有完成:

在:

df.groupby(np.arange(len(df))//10)
for k,g in df.groupby(np.arange(len(df))//10):
    [g.data.apply(get_data) for _, g in df.groupby(np.arange(len(df))//10)]

输出:

----> 7     df = pd.concat(g.data.apply(get_data) for _, g in df2.groupby(np.arange(len(df2))//4))
      8 df

/usr/local/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2290             else:
   2291                 values = self.asobject
-> 2292                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   2293 
   2294         if len(mapped) and isinstance(mapped[0], Series):

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:63307)()

<ipython-input-28-329dbdbb7cdb> in get_data(data)
     62 
     63     r = requests.get('http://api.example.com/api', params=payload, stream = True)
---> 64     json_data = json.dumps(r.json())
     65     data = yaml.load(json_data)
     66   

/usr/local/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs)
    848                     # used.
    849                     pass
--> 850         return complexjson.loads(self.text, **kwargs)
    851 
    852     @property

/usr/local/lib/python3.5/site-packages/simplejson/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw)
    514             parse_constant is None and object_pairs_hook is None
    515             and not use_decimal and not kw):
--> 516         return _default_decoder.decode(s)
    517     if cls is None:
    518         cls = JSONDecoder

/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in decode(self, s, _w, _PY3)
    368         if _PY3 and isinstance(s, binary_type):
    369             s = s.decode(self.encoding)
--> 370         obj, end = self.raw_decode(s)
    371         end = _w(s, end).end()
    372         if end != len(s):

/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in raw_decode(self, s, idx, _w, _PY3)
    398             elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
    399                 idx += 3
--> 400         return self.scan_once(s, idx=_w(s, idx).end())

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

然而,我不明白如何把所有的东西都链在一起后分裂成块。你知道吗


Tags: andinselfjsondfdatagetlen
1条回答
网友
1楼 · 发布于 2024-04-23 06:59:16

您可以创建一个列表来存储接收到的文件,这里g将与原始数据帧相同,但大小更小:

[g.data.apply(get_data) for _, g in df.groupby(np.arange(len(df))//10)]

或者,如果您想对data系列中的每个文本都有一个响应,那么您真正想要的是:

df.data.apply(get_data)

注意,df[["data"]]返回一个数据帧,因此df[["data"]].apply(get_data, axis = 1)将整个列传递给get_data函数。你知道吗

相关问题 更多 >