简体   繁体   中英

Python save images into the folder

I am using python-instagram API and I am displaying some images with searched tag!

Rather than displaying, I want to save those images so that it can be used for further analysis.

Is it possible? I am new to python and using API's.

Here is my code snippet which does this:

@route('/tag_search')
def tag_search(session): 
    access_token = session.get('access_token')
    content = "<h2>Tag Search</h2>"
    if not access_token:
        return 'Missing Access Token'
    try:
        api = client.InstagramAPI(access_token=access_token)
        tag_search, next_tag = api.tag_search(q="catband")
        tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name)
        photos = []
        for tag_media in tag_recent_media:
            photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url())
        content += ''.join(photos)
    except Exception, e:
        print e     

Thanx in advance:)

After some help from comments, and other resources, I found out that since I have URL of the image, I can use it to download!

The library which is used was " urllib " I used a counter variable to save images in the same directory where the file is and in the form of 1.jpg, 2.jpg and so on and so forth.

Here is the modified code:

@route('/tag_search')
def tag_search(session): 
access_token = session.get('access_token')
content = "<h2>Tag Search</h2>"
if not access_token:
    return 'Missing Access Token'
try:
    api = client.InstagramAPI(access_token=access_token)
    tag_search, next_tag = api.tag_search(q="selfie")
    tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name)
    photos = []
    count = 0
    for tag_media in tag_recent_media:
        photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url())
        urllib.urlretrieve(tag_media.get_standard_resolution_url(), `count`+".jpg")
        count = count + 1
    content += ''.join(photos)
except Exception, e:
    print e              

Hope this Helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM