Mrage

QA3

Oct 27th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import urllib.request
  4. import sys
  5. from threading import Thread
  6. import json
  7. count = 1
  8. threadnum = 0
  9. files = []
  10. def JsonWrite(file,alt):
  11.     form = {'url':file, 'alt':alt}
  12.     files.append(form)
  13. def JsonWritefile():
  14.     f = open('names.json','w')
  15.     json.dump(files,f)
  16.     f.close()
  17.     print('Downloaded!!!')
  18.     sys.exit()    
  19. def LoadImage(url,alt):
  20.     global count
  21.     filename = str(count)+'.png'
  22.     count += 1
  23.  
  24.     url = "http:"+url
  25.     f = open(filename,'wb')
  26.     f.write(urllib.request.urlopen(url).read())
  27.     f.close()
  28.     JsonWrite(filename, alt)
  29.     print("[+] {0} file loaded ({1})".format(filename,alt))
  30. def GetPictures(link):
  31.     global count, threadnum
  32.     print('Started thread%i'%threadnum)
  33.     req = requests.get(link)
  34.     soup = BeautifulSoup(req.text,'html.parser')
  35.     images = soup.find_all("img", {"class":"grow-rotate"})
  36.     if len(images)==0:
  37.         print('ZERO LENGTH')
  38.     for image in images:
  39.         LoadImage(image.attrs['src'],image.attrs['alt'])
  40.     threadnum-=1
  41. if __name__ == "__main__":
  42.     if len(sys.argv)<2:
  43.         print("Error: Type links 1-10 \n python scrapskins.py [link1] [link2] [link3] ...")
  44.         sys.exit()
  45.     for param in sys.argv[1:]:
  46.         th = Thread(target=GetPictures, args=(param,))
  47.         th.start()
  48.         threadnum+=1
  49.     while(threadnum!=0):
  50.         pass
  51.     JsonWritefile()
Add Comment
Please, Sign In to add comment