Skip to content

Instantly share code, notes, and snippets.

@tjoen
Last active December 12, 2017 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjoen/4877be6f36d40827995250a439c67fca to your computer and use it in GitHub Desktop.
Save tjoen/4877be6f36d40827995250a439c67fca to your computer and use it in GitHub Desktop.
google image search
import json
from bs4 import BeautifulSoup
import urllib2
request_headers = {
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Referer": "http://thewebsite.com",
"Connection": "keep-alive"
}
def google_image(x):
search = x.split()
search = '+'.join(map(str, search))
url = 'https://www.google.nl/search?as_st=y&tbm=isch&hl=nl&as_q=%s&as_epq=&as_oq=&as_eq=&cr=&as_sitesearch=&safe=active&tbs=iar:s' % search
request = urllib2.Request( url, headers=request_headers)
resp = urllib2.urlopen(request).read()
soup = BeautifulSoup(resp,"html.parser")
links = soup.find_all('div',attrs={"class" : "rg_meta notranslate"})
image = json.loads(links[0].text)
return image['ou']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment