Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from pathlib import Path
- from urllib.request import urlopen, Request
- from time import sleep
- def downloadurl(url):
- print("Downloading album", url)
- dpath=Path(list(filter(None, url.split('/')))[-1])
- dpath.mkdir(exist_ok=True)
- running=True
- pagenum="1"
- headers={"User-agent": 'Mozilla'}
- while running:
- sleep(0.1)
- s=urlopen(Request(url,headers=headers)).read().decode("utf-8")
- idx=s.find("<div class=\"main-image\">")
- pat="<a href="
- idx=s.find(pat,idx)+len(pat)+1
- close=s.find('"',idx)
- imgurl = s[idx:close]
- print(f"Downloading page {pagenum} from {imgurl}")
- with urlopen(Request(imgurl, headers=headers)) as r, (dpath / (pagenum+"."+imgurl.rsplit(".",1)[-1])).open('wb') as f:
- for chunk in iter(lambda: r.read(4096),b''):
- f.write(chunk)
- pat="<a class=\"on-popunder next page-numbers\" href="
- idx=s.find(pat)+len(pat)+1
- close=s.find('"',idx)
- url=s[idx:close]
- pagenum=url.split('/')[-1]
- running = s[close+1:close+6] == '>Next'
- import sys
- if __name__=='__main__':
- if len(sys.argv)>1:
- for url in sys.argv[1:]:
- downloadurl(url)
- else:
- url=input("Wintoddlers, please enter a url:")
- downloadurl(url)
Advertisement
Add Comment
Please, Sign In to add comment