1

I have this code,

import json
from goose import Goose

def extract(url):
    g = Goose()
    article = g.extract(url=url)
    if article.top_image is None:
        return "none" 
    else:
        if article.top_image.src is None:
          return "none"
        else:
            resposne = {'image':article.top_image.src}
            return article.top_image.src

here instead of "none" I want to return image file. In order to upload image file, do I need to save image in my static file and return that as an Image?I've been doing returning image from static file with html whole time but I'm not sure how to do that with python, or is there other way?

3
  • Not sure why you'd want to do this. You have an image URL; you should simply include that image in your URL so that the browser can download it directly from that address. Commented Jan 23, 2016 at 11:13
  • @DanielRoseman yes but for some websites, the image url isn't working. for some reason when i type url for youtube it won't work(show broken image)....not sure why so I'm thinking to put default image if it won't work Commented Jan 23, 2016 at 12:17
  • @DanielRoseman, and for some url the image is just blank white which I don't understand why,,,,do you know why? Commented Jan 23, 2016 at 12:25

1 Answer 1

2

You have the URL of the image resource in article.top_image.src so it should just be a matter of downloading the image and returning it. You can use requests module for the downloading part:

import requests

def extract(url):
    article = Goose().extract(url)
    if article.top_image is None or article.top_image.src is None
        return "none"

    r = requests.get(article.top_image.src)
    return r.content

This will return the actual image data from the function.

Possibly you want to return that image as a HTTP response, in which case you can return this from your view function:

return HttpResponse(extract(url), content_type="image/jpeg")
Sign up to request clarification or add additional context in comments.

3 Comments

this code is basically same as the one I wrote, except simpler. What I'm asking is how do I insert a picture instead of "none"
Basically the same... except that it actually retrieves the image data and returns it from the function as requested in your question. I don't understand what insert means. Insert into what? Into HTML rendered from a Django template? Are you hoping to embed the actual image data into the HTML, or simply add an <image> tag that refers to the image file?
I'm sorry, I wasn't being clear. I made a new post with more detail. stackoverflow.com/questions/34972110/… I'm hoping to return static image there so I can use post.image in html. I can see I wasn't clear here, totally my fault. If you can see the new post I've made, that provided more detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.