Advertisement
Guest User

4chan thread image downloader

a guest
May 24th, 2014
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import urllib2
  3. import os
  4.  
  5. url = raw_input('Thread URL: ')
  6. subfolder = raw_input('Subfolder name: ')
  7. if len(subfolder) > 0:
  8.     path = os.path.abspath('./4chdl/' + subfolder)
  9. elif len(subfolder) == 0:
  10.     path = os.path.abspath('./4chdl')
  11. if not os.path.exists(path):
  12.     os.makedirs(path)
  13.  
  14. page = BeautifulSoup(urllib2.urlopen(url))
  15. for link in page.findAll('a', attrs = {'class' : 'fileThumb'}):
  16.     href = 'http://' + link['href'][2:]
  17.     res = urllib2.urlopen(href)
  18.     data = res.read()
  19.     name = href[-17:]
  20.     filename = os.path.join(path, name)
  21.     if os.path.exists(filename):
  22.         print '%s already exists.' % name
  23.     else:
  24.         f = open(filename, 'wb')
  25.         f.write(data)
  26.         f.close()
  27.         print '%s downloaded' % name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement