1. 程式人生 > >python 標準庫 urllib

python 標準庫 urllib

urllib.request.urlretrieve(url, filename=None)

將url所指向的網路檔案複製到本地。將返回一個tuple,元組()(filename,HTTPMessage),其中filename是下載後的本地檔名,HTTPMessage包含了相關資訊。

>>> import urllib
>>> a=urllib.request.urlretrieve('https://github.com/adventuresinML/adventures-in-ml-code/blob/master/tf_word2vec.py',
'haha') >>> a ('haha', <http.client.HTTPMessage object at 0x000001F4C1095D30>) >>> os.listdir() ['.idea', 'haha', 'scratch.py', 'utils.py', 'venv', 'w2v_embed.py', 'word_embedding.py', '__pycache__'] >>> b = a[1] >>> list(b) ['Server', 'Date', 'Content-Type', 'Transfer-Encoding'
, 'Connection', 'Status', 'Cache-Control', 'Vary', 'Set-Cookie', 'Set-Cookie', 'Set-Cookie', 'X-Request-Id', 'Strict-Transport-Security', 'X-Frame-Options', 'X-Content-Type-Options', 'X-XSS-Protection', 'Expect-CT', 'Content-Security-Policy', 'X-GitHub-Request-Id'] >>> b['Date'] 'Fri, 26 Oct 2018 06:27:04 GMT'