-2

My question is similar to this one but I have a few concerns:

1) How do we query for a photo after typing a few key words

2) How do we take a random photo out of the results

3) How do I display the photo that came out

4) How I can do all of this without opening the browser

I would appreciate any help you could give me. (this is all in python 3)

Manav
  • 29
  • 8
  • 2
    Hi Manav, it would be good if you try to focus your question on a few points, rather than asking them all at once. It would make it easier for people to answer you if you do a bit of research into it first, then post your findings one at a time. – Enthus3d Sep 15 '19 at 14:31
  • I suggested an edit structuring your question a bit better, hopefully it could help you structure your questions more in this format in the future. – Enthus3d Sep 15 '19 at 14:34

1 Answers1

1

Ok, your question is a bit broad for Stack Overflow, so it's best if you ask for one small point at a time. I'll provide some resources below so you know what to start research on, then when you have questions when implementing them, it's best if you post detailed questions about each step.

You can also use this as a way to guide you into constructing your project.

5) IMPORTANT This is actually the most important step: if you are going to be 'scraping' or grabbing a large amount of images from the website, make sure the site you are grabbing from allows you to do it!. Check out this article here about scraping ettiquete, so you don't get into any trouble.

1) For number 1, the process will consist of two steps. One step is accepting the string input from the user. You can take a look at this tutorial here, or search through how to accept and handle inputs from the user. Secondly, you will need to feed that user input into what is called a 'webdriver'. The webdriver allows you to automate a web browser to browse the web for you and query for images (eg. through google images). You can find some resources about the Chrome webdriver here or online.

2) This step is done much easier than step one, just consult on how to extract from urls from the question you posted yourself. link. You could make a list of links from the results and just grab an image from a random link. To add to that, for python 3, the function you need instead is urllib.request.urlopen(url). The best practice would make use of the code as referenced here

import urllib.request
import shutil
...
# Download the file from `url` and save it locally under `file_name`:
with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)

3) This step involves using some libraries to display images from Python, you can find a question detailing it here. This step depends on which OS you are using, but you can find helpful posts on Stack Overflow for them.

4) If you want to do this without opening a browser, search up on how to use the WebDriver without GUI display, either on the web or SO.

Good luck!

Enthus3d
  • 1,727
  • 12
  • 26
  • Thanks Enthus3d that is really helpful but the only thing is that the code that was given in the question from my link does not seem to be working for python 3 as the only attribute of urllib there is in my version is the parse attribute can you suggest a different way of doing it? That would help me a lot. – Manav Sep 15 '19 at 17:31
  • @Manav I did a bit of searching on SO, and found one where they make use of a different part of the Python3 urllib library, as the original one is now deprecated. I added it to my answer under step 2), hope that addresses your concerns :) – Enthus3d Sep 15 '19 at 18:39
  • If you want to just show the picture without saving it, I am not too familiar with that area, but there should be posts online about how to directly process images from urllib :P – Enthus3d Sep 15 '19 at 18:41