Skip to content

Instantly share code, notes, and snippets.

@killertilapia
Last active March 10, 2020 07:07
Show Gist options
  • Save killertilapia/45b6ecabd105e8064282834272158943 to your computer and use it in GitHub Desktop.
Save killertilapia/45b6ecabd105e8064282834272158943 to your computer and use it in GitHub Desktop.
Download images using Python Request module
import requests
picture_request = requests.get(Photo_URL, stream=True)
if picture_request.status_code == 200:
with open("/path/to/image.jpg", 'wb') as f:
for chunk in picture_request.iter_content(chunk_size=512):
f.write(chunk)
# as a function with save_folder
def _download_file(self, target_file, save_folder='dn_temp'):
parsed_url = urlparse(target_file)
save_file_name = os.path.basename(parsed_url.path)
headers = {
'user-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:69.0) Gecko/20100101 Firefox/69.0'
}
r = requests.get(target_file, headers=headers, stream=True)
if r.status_code == 200:
save_path = os.path.join(os.getcwd(), save_folder, save_file_name)
with open(save_path, mode='wb') as dn_img:
for chunk in r.iter_content(512):
dn_img.write(chunk)
return save_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment