gpt4 book ai didi

python - iter_content() 函数到底做了什么?

转载 作者:行者123 更新时间:2023-12-05 08:56:30 25 4
gpt4 key购买 nike

刚刚从自动化无聊的东西书上学习了网页抓取,但我仍然对 iter_content 函数感到困惑?它实际上做了什么?

尝试使用正常方式下载网页:

note = open('download.txt', 'w')
note.write(request)
note.close()

但结果与使用不同:

note = open('download.txt', 'wb')
for chunk in request.iter_content(100000):
note.write(chunk)
note.close()

???

最佳答案

iter_content(chunk_size=1, decode_unicode=False)

Iterates over the response data. When stream=True is set on the request, this avoids reading the content at once into memory for large responses. The chunk size is the number of bytes it should read into memory. This is not necessarily the length of each item returned as decoding can take place.

chunk_size must be of type int or None. A value of None will function differently depending on the value of stream. stream=True will read data as it arrives in whatever size the chunks are received. If stream=False, data is returned as a single chunk.

If decode_unicode is True, content will be decoded using the best available encoding based on the response.

链接 - http://docs.python-requests.org/en/master/api/

在这种情况下,它将每次迭代 100000 字节的响应。

关于python - iter_content() 函数到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846671/

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