-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcraigslist_scraper.py
More file actions
175 lines (136 loc) · 5.99 KB
/
craigslist_scraper.py
File metadata and controls
175 lines (136 loc) · 5.99 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# BY CODEFREDY
# This is wriiten to be compactible with Python 3.6.7
# Importing needed modules for this script
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
import time
import csv
from bs4 import BeautifulSoup
import requests
class CraigslistScraper(object):
def __init__(self, location, max_price, min_price):
self.location = location
self.max_price = max_price
self.min_price = min_price
self.driver = webdriver.Chrome('./chromedriver')
self.sleep = time.sleep(4)
self.url = f"https://{location}.craigslist.org/search/hhh?query=rent&sort=rel&min_price={min_price}&max_price={max_price}&min_bedrooms=3&availabilityMode=0&housing_type=3&housing_type=4&housing_type=5&housing_type=6&housing_type=7&housing_type=8&housing_type=10&housing_type=11&housing_type=12&sale_date=all+dates&lang=en&cc=gb"
self.url2 = f"https://{location}.craigslist.org/search/hhh?query=sale%20by%20owner&sort=rel&min_price{min_price}&max_price={max_price}&min_bedrooms=3&availabilityMode=0&sale_date=all+dates&lang=en&cc=gb"
def forRent(self):
"""Open the Craiglist Search Result For Rent"""
try:
self.driver.get(self.url)
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "searchform")))
print("Page is ready!")
except TimeoutException:
self.driver.get(self.url)
print("BAD NETWORK! RESTART APPLICATION")
def forSale(self):
"""Open the Craiglist Search Result For Sale By Owner"""
try:
self.driver.get(self.url2)
wait = WebDriverWait(self.driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "searchform")))
print("Page is ready!")
except TimeoutException:
self.driver.get(self.url2)
print("loading took too much time")
def extract_titles(self):
"""Scraping Searched Names, Date-posted and Price"""
titles = self.driver.find_elements_by_class_name("result-row")
self.names = []
self.dates = []
self.prices = []
# Spliting the extracted title content to smaller data
for post in titles:
title = post.text.split("$")
if title[0] == '':
title = title[1]
else:
title = title[0]
title = title.split("\n")
price = title[0]
title = title[-1]
title = title.split(" ")
month = title[0]
day = title[1]
title = ''.join(title[2:])
date = month + " " + day
self.dates.append(date)
self.prices.append(price)
self.names.append(title)
def extract_links(self):
"""Scraping Search Results Individual Email Links"""
self.url_list = []
print("Extract links....")
for a in range(500):
self.driver.find_elements_by_class_name("result-row")[a].click()
self.sleep
link = self.driver.current_url
self.sleep
self.url_list.append(link)
self.driver.get(self.url)
self.sleep
def read_forRent_to_csv(self):
"""Reading Scraped Data For Rent To CSV File"""
#Open csv file
with open("CraigslistForRent.csv", "w", newline='') as csvfile:
#Input headers
fieldnames = ['Names', 'Dates', 'Prices', 'Links']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
#Input scraped content
for i in range(500):
writer.writerow({'Names': self.names[i].ljust(50), 'Prices': self.prices[i], 'Dates': self.dates[i], 'Links': self.url_list[i]})
def read_forSale_to_csv(self):
"""Reading Scraped Data For Sale By Owner To CSV File"""
#Open csv file
with open("CraigslistForSaleByOwner.csv", "w", newline='') as csvfile:
#Input headers
fieldnames = ['Names', 'Dates', 'Prices', 'Links']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
#Input scraped content
for i in range(500):
writer.writerow({'Names': self.names[i].ljust(50), 'Prices': self.prices[i], 'Dates': self.dates[i], 'Links': self.url_list[i]})
def quit(self):
"""Exit CraigslistBot"""
self.driver.quit()
while True:
# Choose between For Rent and For Sale
print("""HEY! I AM GOING TO SCRAPE YOUR REQUESTED CONTENT FROM CRAIGSLIST TO AN EXCEL FILE""")
search = input("Reply with '1' To Search By Rent or '2' To Search By For Sale From Owner:-")
if search == '1':
print("All necessary types are checked off!")
location = input("Input Location Here(States Only e.g 'houston'): ")
max_price = input("Input Maximum Price Here: ")
min_price = input("Input Minimum Price Here: ")
scraper = CraigslistScraper(location, max_price, min_price)
print("BOT LOADING........")
scraper.forRent()
scraper.extract_titles()
scraper.extract_links()
scraper.read_forRent_to_csv()
print("DONE!!")
scraper.quit()
elif search == '2':
location = input("Input Location Here: ")
max_price = input("Input Maximum Price Here: ")
min_price = input("Input Minimum Price Here: ")
scraper = CraigslistScraper(location, max_price, min_price)
print("BOT LOADING........")
scraper.forSale()
scraper.extract_titles()
scraper.extract_links()
scraper.read_forSale_to_csv()
print("DONE!!")
scraper.quit()
else:
print("WRONG INPUT!!! TRY AGAIN.")
print("DONE!!")
scraper.quit()