-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBM_selenium.py
156 lines (133 loc) · 6.36 KB
/
FBM_selenium.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#facebook marketplace
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from pymongo import MongoClient
class App:
def __init__(self, email= "", password= "",
path='/Users/keenanburkepitts/Documents/Data_Science/NYC_DSA/final_project'):
self.email = email
self.password = password
self.path = path
self.driver = webdriver.Firefox()
self.main_url = "https://www.facebook.com"
self.client = MongoClient('mongodb://localhost:27017/')
self.driver.get(self.main_url)
self.log_in()
self.used_item_links = []
self.scrape_item_links()
self.scrape_item_details()
self.driver.quit()
def log_in(self):
try:
email_input = self.driver.find_element_by_id("email")
email_input.send_keys(self.email)
sleep(0.5)
password_input = self.driver.find_element_by_id("pass")
password_input.send_keys(self.password)
sleep(0.5)
login_button = self.driver.find_element_by_xpath("//*[@type='submit']")
login_button.click()
sleep(3)
except Exception:
print('Some exception occurred while trying to find username or password field')
def scrape_item_links(self):
marketplace_button = self.driver.find_element_by_xpath('//div[contains(text(), "Marketplace")]')
marketplace_button.click()
#create a list of each section and loop through list
sections = [self.driver.find_element_by_xpath('//div[contains(text(), "Home & Garden")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Entertainment")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Clothing & Accessories")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Family")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Electronics")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Hobbies")]'),
self.driver.find_element_by_xpath('//div[contains(text(), "Vehicles & Bicycles")]')]
for category in sections:
#self.driver.implicitly_wait(300)
category.click()
for i in range(100):
try:
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
sleep(1.5)
except:
pass
full_items_list = self.driver.find_elements_by_xpath("//a[@class='_1oem']")
if self.used_item_links == 0: #False or None?
self.used_item_links = [item.get_attribute('href') for item in full_items_list]
else:
#append or extend or add?
self.used_item_links = self.used_item_links.extend([item.get_attribute('href') for item in full_items_list])
#wait = WebDriverWait(self.driver, 10)
#category_element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, category.get_attribute('class'))))
#category_element.click()
print([item.get_attribute('href') for item in full_items_list])
print(self.used_item_links)
return self.used_item_links
def scrape_item_details(self, used_item_links):
db = LOCAL_USED_ITEMS
for url in used_item_links:
images = []
driver.get(url)
sleep(0.5)
url = url
try:
image_element = driver.find_element_by_xpath('//img[contains(@class, "_5m")]')
images = [image_element.get_attribute('src')]
except:
images = ""
try:
title = driver.find_element_by_xpath('//div[contains(@class, " _50f")]/span').text
except:
title = ""
try:
date_time = driver.find_element_by_xpath('//a[@class="_r3j"]').text
except:
date_time = ""
try:
location = driver.find_element_by_xpath('//span[@class="_7yi"]').text
except:
location = ""
try:
price = driver.find_element_by_xpath('//div[contains(@class, "_5_md")]').text
except:
price = ""
try:
if driver.find_element_by_xpath("//a[@title='More']").is_displayed():
driver.find_element_by_xpath("//a[@title='More']").click()
description = driver.find_element_by_xpath('//p[@class="_4etw"]/span').text
except:
description = ""
try:
previous_and_next_buttons = driver.find_elements_by_xpath("//i[contains(@class, '_3ffr')]")
next_image_button = previous_and_next_buttons[1]
while next_image_button.is_displayed():
next_image_button.click()
image_element = driver.find_element_by_xpath('//img[contains(@class, "_5m")]')
sleep(1)
if image_element.get_attribute('src') in images:
break
else:
images.append(image_element.get_attribute('src'))
except:
pass
db.Facebook_items.insert({ 'Url': url,
'Images': images,
'Title': title,
'Description': description,
'Date_Time': date_time,
'Location': location,
'Price': price,
})
print({ 'Url': url,
'Images': images,
'Title': title,
'Description': description,
'Date_Time': date_time,
'Location': location,
'Price': price,
})
if __name__ == '__main__':
app = App()