-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
62 lines (43 loc) · 1.75 KB
/
scrape.py
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
from random import choice
from time import sleep
from lxml import html
from selenium import webdriver
from settings import IMAGE_URL, IMAGE_XPATH_FORMAT, IMAGE_START_VALUE, IMAGE_END_VALUE, GHOSTDRIVER_LOG_PATH
def get_random_image():
elements = extract_image_elements_from_url(IMAGE_URL, IMAGE_XPATH_FORMAT)
if len(elements) == 0:
print "WARNING: No images could be pulled from source."
return None
random_image = extract_image_from_string(choice(elements), IMAGE_START_VALUE, IMAGE_END_VALUE)
return random_image
def extract_image_elements_from_url(url, xpath_format):
retry_count = 0
page = None
driver = webdriver.PhantomJS(service_log_path=GHOSTDRIVER_LOG_PATH)
while retry_count < 5 and page is None:
try:
driver.get(url)
page = driver.page_source
except:
retry_count += 1
print "Connection failed, retrying..."
print "Retry Count: " + str(retry_count)
continue
elements = []
while len(elements) == 0:
try:
# Scroll down to ensure infinite scroll loads at least a few photos
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(1)
page = driver.page_source
tree = html.fromstring(page)
elements = tree.xpath(xpath_format)
except:
print "Request for image content failed. Aborting :("
raise Exception('Failed to request image content.')
driver.quit()
return elements
def extract_image_from_string(value, start_value, end_value):
start_index = value.find(start_value) + len(start_value)
end_index = value.find(end_value, start_index)
return value[start_index:end_index]