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

标签 python python-3.x

刚刚从自动化无聊的东西书上学习了网页抓取,但我仍然对 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/

相关文章:

Python 3 : Using subprocess Instead Of os. 系统

python - 如何根据字典和另一列之间的匹配创建新的 df 列

python - AWS Cognito 和 Lambda : add federated identity to user pool

python - 使用 Python 通过 ssh 将文件 cat 到远程 bash 脚本

python - 如何制作计数器的子集?

python - 让用户输入 3 个名字,在 Python 中按字母顺序排列

python - 如何返回列表中项目的第一个索引出现?

python - 一次覆盖所有类方法来做同样的事情

Python Selenium Webdriver Wait.Until 显示错误正好需要 2 个参数 3

具有输入函数和多个条件检查的 Python while 循环