Skip to content

Instantly share code, notes, and snippets.

@iloahz
Created December 13, 2012 05:40
Show Gist options
  • Save iloahz/4274318 to your computer and use it in GitHub Desktop.
Save iloahz/4274318 to your computer and use it in GitHub Desktop.
try get an image with better quality from any image given by url. uses google's search by image.
import urllib, urllib2
from cookielib import CookieJar
from urlparse import urlparse, parse_qs
from bs4 import BeautifulSoup
def toFile(s):
f = open('dat.html', 'w')
f.write(s)
f.close()
def enlargeByUrl(url):
cj = CookieJar()
dat = {
}
dat = urllib.urlencode(dat)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('user-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11')]
#res = opener.open('https://www.google.com/ncr', None, 10)
#res = opener.open('https://www.google.com/imghp', None, 10)
res = opener.open('https://www.google.com/searchbyimage?' + urllib.urlencode({'image_url' : url, 'safe' : 'off'}), None, 10)
soup = BeautifulSoup(''.join(res.read()))
t = soup.find('a', text = 'All sizes')
#print 'https://www.google.com' + t['href'] + '&biw=1280&bih=643'
#print 'https://www.google.com/search?hl=en&safe=off&tbo=d&q=tokyo+juliet+ost&tbm=isch&tbs=simg:CAQSZRpjCxCo1NgEGgIICwwLELCMpwgaPAo6CAESFPgEiQSLBIMEhQTkBeUF_1wT5BMkFGiDne1kJSlZpR4VVucKY_1Cq3xnfJmQamWkedUW6KS8hQCgwLEI6u_1ggaCgoICAESBMKuf2oM&sa=X&ei=NYrIUImRFZHRmAWZloF4&ved=0CCcQ2A4oAA&biw=1280&bih=643&sei=W4rIUNHPLOyKmQXp_IDAAQ'
res = opener.open('https://www.google.com' + t['href'] + '&biw=1280&bih=760')
soup = BeautifulSoup(''.join(res.read()))
t = soup.find('div', class_ = 'rg_di')
p = urlparse(t.a['href'])
p = parse_qs(p.query)
return p['imgurl'][0]
if __name__ == '__main__':
url = 'http://img3.douban.com/mpic/s1687633.jpg'
# print urllib.urlencode({'url' : url, 'sag' : 'abc'})
enlargeByUrl(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment