Advertisement
575

Untitled

575
Dec 21st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import re
  4. import urllib3
  5. import os
  6. import json
  7.  
  8. def get_soup(url,header):
  9. return BeautifulSoup(urllib3.urlopen(urllib3.Request(url,headers=header)),'html.parser')
  10.  
  11.  
  12. query = raw_input("query image")
  13. image_type="ActiOn"
  14. query= query.split()
  15. query='+'.join(query)
  16. url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
  17. print(url)
  18. DIR="Pictures"
  19. header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"
  20. }
  21. soup = get_soup(url,header)
  22.  
  23.  
  24. ActualImages=[]
  25. for a in soup.find_all("div",{"class":"rg_meta"}):
  26. link , Type =json.loads(a.text)["ou"] ,json.loads(a.text)["ity"]
  27. ActualImages.append((link,Type))
  28.  
  29. print("there are total" , len(ActualImages),"images")
  30.  
  31. if not os.path.exists(DIR):
  32. os.mkdir(DIR)
  33. DIR = os.path.join(DIR, query.split()[0])
  34.  
  35. if not os.path.exists(DIR):
  36. os.mkdir(DIR)
  37.  
  38. for i , (img , Type) in enumerate( ActualImages):
  39. try:
  40. req = urllib3.Request(img, headers={'User-Agent' : header})
  41. raw_img = urllib3.urlopen(req).read()
  42.  
  43. cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
  44. print(cntr)
  45. if len(Type)==0:
  46. f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+".jpg"), 'wb')
  47. else :
  48. f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+"."+Type), 'wb')
  49.  
  50.  
  51. f.write(raw_img)
  52. f.close()
  53. except Exception as e:
  54. print("could not load : " + img)
  55. print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement