Advertisement
Guest User

Untitled

a guest
Jan 11th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import urllib.request, urllib.parse, urllib.error
  4. import sys
  5. import os
  6.  
  7.  
  8. def download_media(url):
  9. resp = requests.get(url)
  10. if resp.status_code == 200:
  11. soup = BeautifulSoup(resp.text, 'html.parser')
  12. tags_with_media = soup.find_all('a',class_='post__image-link')
  13. for tag in tags_with_media:
  14. file_link = 'https://2ch.hk' + tag['href']
  15. destination = './downloads/' + file_link.rsplit('/',1)[1]
  16. print('start: ', file_link)
  17. urllib.request.urlretrieve(file_link, destination)
  18. print('end: ', file_link)
  19. else:
  20. print('connection error')
  21.  
  22. if __name__ == '__main__':
  23. try:
  24. os.mkdir('downloads')
  25. print('the download folder was created')
  26. except:
  27. print('the download folder is used')
  28. URL = str(sys.argv[1])
  29. download_media(URL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement