Skip to content

Instantly share code, notes, and snippets.

Created September 26, 2012 12:41
Show Gist options
  • Save qtproduction/3787790 to your computer and use it in GitHub Desktop.
Save qtproduction/3787790 to your computer and use it in GitHub Desktop.
Retrive website from Google Cache without blocking IP
#Retrive old website from Google Cache. Optimized with sleep time, and avoid 504 error (Google block Ip send many request).
#Programmer: Kien Nguyen - QTPros
#change search_site and search_term to match your requirement
import urllib, urllib2
import re
import socket
import os, errno, os.path
import time
import random, math
#import MySQLdb
import imp;
#adjust the site here
search_term="site:" + search_site
#mysql = imp.load_source("MySQLConnector", "").MySQLConnector()
def mkdir_p(path):
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
else: raise
def main():
headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv: Gecko/20070515 Firefox/'}
url = ""+search_term
regex_cache = re.compile(r'<a href="([^"]*)"[^>]*>Cached</a>')
regex_next = re.compile('<a href="([^"]*)"[^>]*><span[^>]*>[^<]*</span><span[^>]*>Next</span></a>')
regex_url = re.compile(r'search\?q=cache:[\d\w-]+:([^%]*)')
# regex_title = re.compile('<title>([\w\W]+)</title>')
# regex_time = re.compile('page as it appeared on ([\d\w\s:]+)')
regex_pagenum = re.compile('<a href="([^"]*)"[^>]*><span[^>]*>[^<]*</span>([\d]+)')
#this is the directory we will save files to
path = os.path.dirname(os.path.abspath(__file__)) + '\\' + search_site
# path = os.path.dirname(os.path.abspath(__file__))
counter = 0
pagenum = int(math.floor(len([name for name in os.listdir(path)]) / 10) + 1)
max_goto = 0;
more = True
if (pagenum > 1):
while (max_goto < pagenum):
req = urllib2.Request(url, None, headers)
page = urllib2.urlopen(req).read()
goto = regex_pagenum.findall(page)
# print goto
for goto_url, goto_pagenum in goto:
goto_pagenum = int(goto_pagenum)
if (goto_pagenum == pagenum):
url = "" + goto_url.replace('&amp;', '&')
max_goto = pagenum
elif (goto_pagenum < pagenum and max_goto < goto_pagenum):
max_goto = goto_pagenum
url = "" + goto_url.replace('&amp;', '&')
random_interval = random.randrange(5, 20, 1)
print "sleeping for: " + str(random_interval) + " seconds"
print "going to page: " + str(max_goto)
print url
#Send search request to google with pre-defined headers
req = urllib2.Request(url, None, headers)
#open the response page
page = urllib2.urlopen(req).read()
#find all cache in the page
matches = regex_cache.findall(page)
#loop through the matches
for match in matches:
#find the url of the page cached by google
the_url = regex_url.findall(match)
the_url = the_url[0]
the_url = the_url.replace('http://', '')
the_url = the_url.strip('/')
the_url = the_url.replace('/', '-')
#if href doesn't start with http insert http before
if not match.startswith("http"):
match = "http:" + match
if (not the_url.endswith('html')):
the_url = the_url + ".html"
#if filename "$url"[.html] does not exists
if not os.path.exists(search_site + "/" + the_url):
tmp_req = urllib2.Request(match.replace('&amp;', '&'), None, headers)
tmp_page = urllib2.urlopen(tmp_req).read()
f = open(search_site + "/" + the_url, 'w')
print counter, ": " + the_url
#comment out the code below if you expect to crawl less than 50 pages
random_interval = random.randrange(15, 20, 1)
print "sleeping for: " + str(random_interval) + " seconds"
except urllib2.HTTPError, e:
print 'Error code: ', e.code
#now check if there is more pages
match =
if match == None:
more = False
url = ""'&amp;', '&')
if __name__=="__main__":
Copy link

mav1 commented Jun 17, 2013

Hello! I tried to run this script, but it only makes directory (empty). Maybe, script is old? Have you any more actual version? Thx

Copy link

The problem is that Google no longer provides cache: links. It needs to be re-written to take a single URL at webcache.* and then rewrite the URLs in the html to get it's next thing to fetch.

I just switched to using Warrick:

Copy link

rzr commented Mar 22, 2020

Any updated script ?

Copy link

ghost commented Jan 11, 2022

direct access the web-cache + alternatives

you can still access pages manually (not all websites are available!):

  • remove protocol and ://, use whatever equivalent in your desired language to JavaScript's encodeURIComponent is and add at the end of
+ you will never get blocked, 
+ no need to query, 
+ no need to filter-out HTML/DOM structure tree, 
+ there is no captcha, and very few minimal restrictions (mostly just rate as with most google servers).
+ if you get error code (5**/4**) or empty response 'body', it simply means there isn't entry in the cache for that website.

as this is essentially a mirror/archive,
there are alternatives you can fall-back to (or switch as your main scraping target, as both are superior to google-cache)

  • encodeURIComponent your URL (as is) and add at the end of*/ (can change 2019 for earlier results, various 'api's are available to narrow down results).

  • same as above to

  • you can also (although not intended to be used this way) the google translate.
    you don't have to actually translate, your target language can always be English, even if the original website is already in English.

the result will be the website under a different (google) domain,

start with and your website (escaped, as before),
now days it will redirect to the new interface,

for example:

will be

which will (now days) redirect to:

there is a bonus, in which you will browse old HTTP website through the mirror-server HTTPS ;]

if you can't find the article/website you are looking for, just save it yourself.

and then (you need to wait until those will end mirroring the website) simply try accessing the website again.

(you can't trigger yourself the google cache archiving process..)

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