requests模块的前代是urllib模块,传入参数headers、cookie、data什么的肯定是requests好使,但是却没有urllib.request.urlretrieve这个方法,
urlretrieve(url, filename=None,reporthook=None, params=None,)
传入url跟文件路径即可下载文件,requests次次都得自己手动编写,我觉得太麻烦了,而且它还有个回调函数,我试着能不能把这个urlretrieve方法移植到requests模块来
要点:1.如何找到自己想要的python模块呢?cmd上打path,找出python的,然后CTRL+F
2.下载文件的实质是 contextlib.closing打开网页--->with open文件--->写入
3.reporthook回调函数的实质就是 把文件一段段写入文件时把3个参数(每次写入bytes的数量、次数、headers得到的总大小size)传出去,让回调函数处理
4.原来现在的模块,都是其他py文件写好方法,然后把其方法传入__init__.py这个文件的
5.r.iter_content()的应用
进入urllib文件夹,在request文件中找到urlretrieve方法,具体如下
def urlretrieve(url, filename=None, reporthook=None, data=None): """ Retrieve a URL into a temporary location on disk. Requires a URL argument. If a filename is passed, it is used as the temporary file location. The reporthook argument should be a callable that accepts a block number, a read size, and the total file size of the URL target. The data argument should be valid URL encoded data. If a filename is passed and the URL points to a local resource, the result is a copy from local file to new file. Returns a tuple containing the path to the newly created data file as well as the resulting HTTPMessage object. """ url_type, path = splittype(url) #分析网页的,忽略 with contextlib.closing(urlopen(url, data)) as fp: #打开网页 headers = fp.info() #头 # Just return the local path and the "headers" for file:// # URLs. No sense in performing a copy unless requested. if url_type == "file" and not filename: return os.path.normpath(path), headers #忽略 # Handle temporary file setup. if filename: