Skip to content

Instantly share code, notes, and snippets.

@whitead
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save whitead/98df7898995f4c4c54d3 to your computer and use it in GitHub Desktop.

Select an option

Save whitead/98df7898995f4c4c54d3 to your computer and use it in GitHub Desktop.
Gif Magic
from IPython.core.magic import (register_line_magic)
import mechanize
from IPython.display import Image
from bs4 import BeautifulSoup
import urllib
#string=raw_input("What are you looking for?")
def image_results(string):
options = {'q': string, 'tbm': 'isch', 'tbs': 'itp:animated'}
search_url='https://www.google.com/search?' + urllib.urlencode(options)
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')]
br.open(search_url)
response=br.response()
return response.read()
def parse_results(html):
soup = BeautifulSoup(html)
result = []
for item in soup.find_all('div', class_='rg_di rg_el'):
href = item.a['href']
start = href.find('imgurl') + 7
end = href.find('imgrefurl') - 1
result.append(href[start:end])
return result
@register_line_magic
def gif(line, link_number=0):
html = image_results(line)
links = parse_results(html)
url = links[link_number]
return Image(url=url,width=400)
# We delete these to avoid name conflicts for automagic to work
del gif

Prequisites: If you're on windows, you'll need to view file extensions. Go to the start menu and type Show or hide file extensions. The first search result will be a control panel setting for accomplishing this. There are checkboxes there. Make sure Show hidden files, folders, and drives is checked and Hide extensions for known file types is NOT checked.

Install Beautiful Soup 4. Type the command below in a command prompt (where you run ipython notebook)

conda install -c https://binstar.org/ioos beautifulsoup4

Install Mechanize:

conda install -c https://conda.binstar.org/dhirschfeld mechanize

Install the gif magic code by finding where your ipython settings directory is located. Execute this on the comamnd line:

ipython locate

Go to the folder it said. There should be a folder called profile_default. Go to it. Next go to a folder startup in profile_default. This is where you need to save the gif_magic.py file below. Click the "raw" button below for that file and ctrl-s or cmd-s to save it. Save it in the folder mentioned above. On windows, it will call it gif_magic.py.txt. Rename it to gif_magic.py.

Now restart python and try typing "%gif success". If you see a funny gif: Good job, you're done. If it says %gif is not installed it means your file is called the wrong thing or you downloaded it to the wrong spot. If it does nothing after waiting a few seconds, it means you probably didn't successfully install one of the packages above. Open a new command window, type ipython. Once ipython (not ipython notebook!) starts up, type %gif and see what the error is.

Have fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment