Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. import urllib.parse
  5.  
  6. Cookie = 'ๅกซ่‡ชๅทฑ็š„'
  7.  
  8. headers = {
  9. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
  10. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
  11. 'Accept-Encoding':'gzip, deflate',
  12. 'Connection': 'keep-alive',
  13. 'Cookie': Cookie
  14. }
  15.  
  16. def fetch(url):
  17. session = requests.Session()
  18. respone = session.get(
  19. url,
  20. headers=headers,
  21. allow_redirects=False
  22. )
  23. return respone
  24.  
  25. url_front = 'http://iclass.tku.edu.tw/api/uploads/'
  26. url_end = '/blob?preview=true'
  27. start = 0
  28. def download(resp, file_name):
  29. filetype = file_name.split('.')[1]
  30. try:
  31. with open('./download/'+filetype+'/'+file_name, "wb") as file:
  32. file.write(resp.content)
  33. except (RuntimeError, TypeError, NameError, ValueError, KeyError, FileNotFoundError):
  34. with open('./download/other/'+file_name, "wb+") as file:
  35. file.write(resp.content)
  36.  
  37.  
  38. for number in range(start, start+300000):
  39. resp = fetch(url_front+str(number)+url_end)
  40. # print(resp.status_code)
  41. if resp.status_code != 404:
  42. if resp.status_code == 302:
  43. try:
  44. url = resp.headers['Location']
  45. filename = urllib.parse.unquote(re.split(r"=", url)[-1])
  46. resp = fetch(url)
  47. download(resp, filename)
  48. print('[Success]', number, "file: "+filename)
  49. except:
  50. print('[Failed]', number)
  51. elif resp.status_code == 200:
  52. try:
  53. filename = str(number)+".png"
  54. download(resp, filename)
  55. print('[Success]', number, "file: "+filename)
  56. except:
  57. print('[Failed]', number)
  58. elif(number%500) == 0:
  59. print('[Failed]', number)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement