Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifmtail
- import os
- import sys
- import requests
- from time import sleep
- from bs4 import BeautifulSoup
- session = requests.Session()
- session.headers.update({
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
- })
- def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
- formatStr = "{0:." + str(decimals) + "f}"
- percent = formatStr.format(100 * (iteration / float(total)))
- filledLength = int(round(barLength * iteration / float(total)))
- bar = '█' * filledLength + '-' * (barLength - filledLength)
- sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix))
- if iteration == total:
- sys.stdout.write('\n')
- sys.stdout.flush()
- def createDir(nameFolder):
- os.mkdir(nameFolder)
- os.chdir(nameFolder)
- def saveWeb(url):
- response = session.get(url)
- nameDir = url.split('/')[-1].split('.')[0]
- createDir(nameDir)
- return response.text
- def parseWeb(text):
- listFile = list()
- soup = BeautifulSoup(text, 'html.parser')
- for link in soup.find_all('div', {'class': 'image-link'}, 'a'):
- listFile.append(link.a.get('href'))
- return listFile
- def downloadFile(url):
- head = 'https://2ch.hk'
- items = list(range(0, len(url)))
- i = 0
- l = len(items)
- for part in items:
- r = requests.get(head+url[part])
- if r.status_code == 200:
- file = open(url[part].split('/')[-1], 'wb')
- file.write(r.content)
- sleep(0.1)
- i += 1
- printProgress(i, l, prefix = 'Progress:', suffix = '', barLength = 50)
- if len(sys.argv) == 2:
- try:
- data = saveWeb(sys.argv[1])
- downloadFile(parseWeb(data))
- except:
- print('error... url not found')
- else:
- print('pls example: dwach.py https://2ch.hk/b/res/%RANDOM_NUMBERS%.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement