|
| 1 | +# Scraping Drop Project By Shravan |
| 2 | + |
| 3 | +# Importing Libraries |
| 4 | +import mechanicalsoup |
| 5 | +import os |
| 6 | +import wget |
| 7 | + |
| 8 | +browser = mechanicalsoup.StatefulBrowser() |
| 9 | +url = "https://www.google.com/imghp?hl=en" |
| 10 | + |
| 11 | +browser.open(url) |
| 12 | +# get HTML |
| 13 | +browser.get_current_page() |
| 14 | + |
| 15 | +# target the search input |
| 16 | +browser.select_form() |
| 17 | + |
| 18 | +# searching for a term |
| 19 | +search_term = input("Enter a search term: ") |
| 20 | +browser["q"] = search_term |
| 21 | + |
| 22 | +# submitting the search |
| 23 | +response = browser.submit_selected() |
| 24 | + |
| 25 | +# opening of URL |
| 26 | +new_url = browser.get_url() |
| 27 | +browser.open(new_url) |
| 28 | +print(new_url, "\n") |
| 29 | + |
| 30 | +# get HTML code |
| 31 | +page = browser.get_current_page() |
| 32 | +all_images = page.find_all('img') |
| 33 | + |
| 34 | +# target the source attributes of image |
| 35 | +image_source = [] |
| 36 | +for image in all_images: |
| 37 | + image = image.get('src') |
| 38 | + image_source.append(image) |
| 39 | + |
| 40 | +image_source[5:25] |
| 41 | +image_source = [image for image in image_source if image.startswith('https')] |
| 42 | +image_source[5:] |
| 43 | + |
B0A6
| 44 | +path = os.getcwd() |
| 45 | +# just adding s to the end of the path name |
| 46 | +path = os.path.join(path, search_term + "s") |
| 47 | + |
| 48 | +typechoice = input("Enter the type of image you want to download (jpg or png): ") |
| 49 | +if typechoice == "jpg": |
| 50 | + imgtype = ".jpg" |
| 51 | +elif typechoice == "png": |
| 52 | + imgtype = ".png" |
| 53 | +else: |
| 54 | + print("Invalid type") |
| 55 | + |
| 56 | +# creating the directory |
| 57 | +os.mkdir(path) |
| 58 | +print('Your Query images are stored here: ', path) |
| 59 | + |
| 60 | +# Saving the images according to type of image |
| 61 | +counter = 0 |
| 62 | +for image in image_source: |
| 63 | + save_as = os.path.join(path, search_term + str(counter) + imgtype) |
| 64 | + wget.download(image, save_as) |
| 65 | + counter += 1 |
0 commit comments