Monday, 2 February 2015

python 3.4.2 part 12 downlaoding an image


First install beautifulscraper

pip install beautifulscraper

 

create a new file downimage.py


/******************************/
       

import urllib.request
import random
def download_image(url):
    file_name=random.randrange(1,1000)
    full_file_name=str(file_name)+".jpg"
    urllib.request.urlretrieve(url,full_file_name)
download_image("https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg")

/***************************************/
run it
1.random is used select a random number
2.first we create download_image(url) function
3.to give a name for downloaded image, we use random function
4.str(file_name) is used to convert  "file_name( number )" to string
5.urllib.request.urlretrieve is a function used to download the image
6."https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7r5YcS7A_m8OAUsgT7W9M7HUm0vR7BD1Sv2jNnbgaP0tlPpuogQjzQeqlsQK0BRK3YMc4pLWl6m3_JJ_DrvjdUBfLCtkvOv0YFlkLjmB4xTcxLr_cn1uSPsXPPM71e50uf9gQGU3FrvY/s1600/2.jpg" this url will pass to def download_image(url):



No comments:

Post a Comment