Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Usage: 4c.py "http://boards.4chan.org/*/thread/*" "/home/user/download/location"
- import sys
- import urllib2
- import re
- import math
- import os
- import HTMLParser
- import errno
- from BeautifulSoup import BeautifulSoup
- def mspexists(path):
- try:
- os.makedirs(path)
- return 0
- except OSError as exception:
- if exception.errno != errno.EEXIST:
- raise
- h=HTMLParser.HTMLParser()
- uagent='Mozilla Firefox'
- dpath=str(sys.argv[2])
- mspexists(dpath)
- req=urllib2.Request(str(sys.argv[1]), headers={'User-Agent' : uagent})
- html=urllib2.urlopen(req)
- soup=BeautifulSoup(html)
- imgs=soup.findAll("div", {"class":"file"})
- cimg=len(imgs)
- cc=0
- print("Found: %s images! Starting download." % cimg)
- for img in imgs:
- cc+=1
- i=("http:%s" % img.a['href'])
- fname = img.a.text
- if str(img.a).find("title=")==-1:
- pass
- else:
- j = (str(img.a).split('title="')[1])
- fname=(str(j).split('"')[0])
- fname=h.unescape(fname)
- req=urllib2.Request(i,headers={'User-Agent' : uagent})
- u=urllib2.urlopen(req)
- if os.path.exists(('%s/%s' % (dpath,fname))):
- print("[%s] %s" % (("%s/%s" % (cc,cimg)),"File already exists - Skipping"))
- else:
- print("[%s] %s" % (("%s/%s" % (cc,cimg)),fname))
- sFile = open(('%s/%s' % (dpath,fname)),'w')
- sFile.write(u.read())
- sFile.close()
- print('All done!')
Add Comment
Please, Sign In to add comment