MCPcopy Create free account
hub / github.com/ActiveState/code / grab

Function grab

recipes/Python/302700_Grab_document_images/recipe-302700.py:25–59  ·  view source on GitHub ↗
(wurl, outdir, wfile, wgif, lgif, cachedir = 'cache',
         tmpfile = 'tmp.htm')

Source from the content-addressed store, hash-verified

23
24# Main procedure
25def grab(wurl, outdir, wfile, wgif, lgif, cachedir = 'cache',
26 tmpfile = 'tmp.htm'):
27 global imre, gifre
28 imre = re.compile(wgif)
29 gifre = re.compile(lgif)
30 # path to temporary file
31 tmpf = path.join(cachedir,tmpfile)
32 print "Retrieving page..."
33 download(wurl, tmpf)
34 f = file(tmpf,'r')
35 s = f.read()
36 f.close()
37 all = imre.findall(s)
38 res = []
39 res2 = []
40 # Fill up result list
41 for i in all:
42 if i not in res:
43 res.append(i)
44 res2.append(gifre.findall(i)[0])
45 result = zip(res, res2)
46
47 # Replace web links with local links
48 ns = re.sub(wgif,
49 gifsub, s)
50 f = file(path.join(outdir,wfile),'wb')
51 f.write(ns)
52 f.close()
53
54 # Download images
55 for i in result:
56 if not path.exists(path.join(outdir,i[1])):
57 download(i[0], path.join(outdir,i[1]))
58
59 print "Done."
60
61if __name__ == '__main__':
62 # Document URL

Callers 1

recipe-302700.pyFile · 0.85

Calls 10

downloadFunction · 0.70
compileMethod · 0.45
joinMethod · 0.45
readMethod · 0.45
closeMethod · 0.45
findallMethod · 0.45
appendMethod · 0.45
subMethod · 0.45
writeMethod · 0.45
existsMethod · 0.45

Tested by

no test coverage detected