But this works well only for small files. If this seems too complicated, you may want to go simpler and store the whole download in a bytes object and then write it to a file. With (url) as response, open(file_name, wb) as out_file: So the most correct way to do this would be to use the function to return a file-like object that represents an HTTP response and copy it to a real file using pyfileobj. tmp/tmpb48zma.txt) in the `file_name` variable:įile_name, headers = (url)īut keep in mind that urlretrieve is considered legacy and might become deprecated (not sure why, though). # Download the file from `url`, save it in a temporary directory and get the # Download the file from `url` and save it locally under `file_name`: The easiest way to download and save a file is to use the function: import urllib.request Text = code(utf-8) # a `str` this step cant be used if data is binary If you want to obtain the contents of a web page into a variable, just read the response of : import urllib.requestĭata = response.read() # a `bytes` object
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |