-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (47 loc) · 1.78 KB
/
Copy pathmain.py
File metadata and controls
63 lines (47 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from selenium import webdriver
from selenium.webdriver.common.by import By
import requests
import io
from PIL import Image
import time
import names
import pandas as pd
PATH = "/Users/belle/Desktop/chromedriver"
wd = webdriver.Chrome(PATH)
def get_images_from_google(wd, delay, max_images):
def scroll_down(wd):
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(delay)
url = "https://www.google.com/search?q=famous+people&tbm=isch&ved=2ahUKEwjn9uDZkpr3AhUaAjQIHZpUDSIQ2-cCegQIABAA&oq=famous+people&gs_lcp=CgNpbWcQAzIHCAAQsQMQQzIICAAQgAQQsQMyCAgAEIAEELEDMggIABCABBCxAzIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDoECAAQQ1CQB1iqEmCnE2gAcAB4AIAB5wGIAZ8LkgEFNi41LjGYAQCgAQGqAQtnd3Mtd2l6LWltZ8ABAQ&sclient=img&ei=NohbYueDOZqE0PEPmqm1kAI&bih=622&biw=1316&rlz=1C5CHFA_enHK929HK930#imgrc=nUozg7xxKlE56M"
wd.get(url)
image_urls = set()
skips = 0
while len(image_urls) + skips < max_images:
scroll_down(wd)
thumbnails = wd.find_elements(By.CLASS_NAME, "Q4LuWd")
for img in thumbnails[len(image_urls) + skips:max_images]:
try:
img.click()
time.sleep(delay)
except:
continue
images = wd.find_elements(By.CLASS_NAME, "n3VNCb")
for image in images:
if image.get_attribute('src') in image_urls:
max_images += 1
skips += 1
break
if image.get_attribute('src') and 'http' in image.get_attribute('src'):
image_urls.add(image.get_attribute('src'))
print(f"Found {len(image_urls)}")
return image_urls
urls = get_images_from_google(wd, 1, 50)
print(urls)
wd.quit()
random_name_set = {names.get_full_name() for i in range(50)}
df2 = pd.DataFrame(urls)
df1 = pd.DataFrame(random_name_set)
final = pd.concat([df1, df2], axis = 1)
print(final)
# export to csv and without index
final.to_csv('final.csv', index = False)