-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacebookScrapper.py
More file actions
368 lines (316 loc) · 18.1 KB
/
FacebookScrapper.py
File metadata and controls
368 lines (316 loc) · 18.1 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import pandas as pd
from bs4 import BeautifulSoup
import json
import time
import numpy as np
from tqdm import tqdm
from typing import *
import re
import os
import glob
from IPython.display import clear_output
# Scrapping and crawling modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from user_agent import generate_user_agent
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from datetime import datetime, timedelta
from urllib.parse import quote
import base64
import json
import locale
locale.setlocale(locale.LC_TIME, 'id_ID.UTF-8')
def encode_params(params: str) -> str:
'''
Encode the parameters to base64 as facebook url uses it for its search parameter
'''
return base64.b64encode(params.encode('utf-8')).decode('utf-8')
def betterWait(value: int):
'''
Just a glorified time.sleep() function
'''
for i in tqdm(range(value), desc="Waiting"):
time.sleep(1)
clear_output()
def generateDailyBackwards(startDate: str, endDate: str) -> List[str]:
'''
PARAMS:
- startDate: str, format 'YYYY-MM-DD'
- endDate: str, format 'YYYY-MM-DD'
RETURN:
- list of date strings from startDate to endDate in reverse order
'''
startDate = datetime.strptime(startDate, '%Y-%m-%d')
endDate = datetime.strptime(endDate, '%Y-%m-%d')
current_date = startDate
day_ranges = []
while current_date <= endDate:
day_ranges.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(days=1)
return day_ranges[::-1]
def emptyCSV():
'''
This function will check whether there are empty csv files in the savepoints folder
'''
csvFiles = [i for i in os.listdir("savepoints") if i.endswith(".csv")]
temp = []
for i in csvFiles:
with open(f'savepoints/{i}', "r", encoding='utf-8', errors='ignore') as f:
if len(f.read().split("\n")) == 2:
temp.append(i)
return temp
# Credentials
with open('Credentials/Facebook.json') as f:
credentials = json.load(f)
EMAIL = credentials['Email']
PASSWORD = credentials['Pass']
# TODO: Simplify it into OOP and handle if tab reaches its end and can't be found. Lol!
class facebookScraper:
def __init__(self):
self.theEntireDataset = pd.DataFrame({"User": [], "Post": [], "Time": [],
"Likes": [], "Comments": [], "Shares": [] })
def startAndLogin(self):
'''
This function will start the driver and login to facebook
'''
loginURL = 'https://www.facebook.com/'
# Initialize the driver
usergAgent = generate_user_agent(device_type="desktop", os="win", navigator="chrome", platform="win")
options = Options()
options.add_argument(f'user-agent={usergAgent}')
self.driver = webdriver.Chrome()
self.driver.get(loginURL)
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//form[@data-testid="royal_login_form"]')))
self.driver.find_element(By.XPATH, '//input[@data-testid="royal_email"]').send_keys(EMAIL)
self.driver.find_element(By.XPATH, '//input[@name="pass"]').send_keys(PASSWORD)
time.sleep(2)
self.driver.find_element(By.XPATH, '//button[@data-testid="royal_login_button"]').click()
betterWait(10)
print("Logged in")
print("Please disallow notification :)")
def searchQuery(self, search: str, startDate: str, endDate: str, recent: Literal[True, False]):
'''
This function will search the query and filter the date
PARAMS
- search: str - The search query
- startDate: str - The start date of the search
`YYYY-MM-DD`
- endDate: str - The end date of the search
`YYYY-MM-DD`
'''
URL = f'https://www.facebook.com/search/posts?q={search}&filters='
# This will crawl the search query + encode it before hand
if any ([startDate, endDate]):
year = startDate.split('-')[0]
if recent:
URL += encode_params(f'{{"rp_creation_time:0":"{{\\"name\\":\\"creation_time\\",\\"args\\":\\"{{\\\\\\"start_year\\\\\\":\\\\\\"{year}\\\\\\",\\\\\\"start_month\\\\\\":\\\\\\"{year}-1\\\\\\",\\\\\\"end_year\\\\\\":\\\\\\"{year}\\\\\\",\\\\\\"end_month\\\\\\":\\\\\\"{year}-12\\\\\\",\\\\\\"start_day\\\\\\":\\\\\\"{startDate}\\\\\\",\\\\\\"end_day\\\\\\":\\\\\\"{endDate}\\\\\\"}}\\"}}", "recent_posts:0":"{{\\"name\\":\\"recent_posts\\",\\"args\\":\\"\\"}}"}}')
else:
URL+= encode_params(f'{{"rp_creation_time:0":"{{\\"name\\":\\"creation_time\\",\\"args\\":\\"{{\\\\\\"start_year\\\\\\":\\\\\\"{year}\\\\\\",\\\\\\"start_month\\\\\\":\\\\\\"{year}-1\\\\\\",\\\\\\"end_year\\\\\\":\\\\\\"{year}\\\\\\",\\\\\\"end_month\\\\\\":\\\\\\"{year}-12\\\\\\",\\\\\\"start_day\\\\\\":\\\\\\"{startDate}\\\\\\",\\\\\\"end_day\\\\\\":\\\\\\"{endDate}\\\\\\"}}\\"}}"}}')
elif recent:
URL += encode_params('{"recent_posts:0":"{\\"name\\":\\"recent_posts\\",\\"args\\":\\"\\"}"}')
self.driver.get(URL)
time.sleep(10)
def reelHandling(self):
# This will handle reel post
theReel = self.post.find_element(By.XPATH, f'.//a[@aria-label="Buka reel di Reels Viewer"]')
# This to get user info and time of the post
try:
# This checks if it's sent from individual account
upperInfos = theReel.find_element(By.XPATH, './div[1]/div[3]/div/div/div[1]/div/div/div[2]/div')
User = upperInfos.find_element(By.XPATH, './div[1]/div[1]/h4').text
hover = ActionChains(self.driver).move_to_element(upperInfos.find_element(By.XPATH, './div[2]/span/span/span/span[2]'))
for i in range(50):
hover.perform()
theTime = self.driver.find_element(By.XPATH, '//div[@class="__fb-dark-mode"]//span').text
theTime = datetime.strptime(theTime, '%A, %d %B %Y pada %H.%M')
# This to get ammount of like, comment, and share
lowerInfps = theReel.find_element(By.XPATH, './div[2]/div/div/div')
like = lowerInfps.find_element(By.XPATH, './div[3]').text if lowerInfps.find_element(By.XPATH, './div[3]').text != '' else 0
comment = lowerInfps.find_element(By.XPATH, './div[4]').text if lowerInfps.find_element(By.XPATH, './div[4]').text != '' else 0
share = lowerInfps.find_element(By.XPATH, './div[5]').text if lowerInfps.find_element(By.XPATH, './div[5]').text != '' else 0
try:
# If it's nested
theReel.find_element(By.XPATH, './div[1]/div[2]/div/div/div[2]/span//object[@type = "nested/pressable"]/div[@role = "button"]').click()
except:
pass
# This to get the post
theText = theReel.find_element(By.XPATH, './div[1]/div[2]/div/div/div[2]/span').text
except:
# This checks if it's sent from a group
upperInfos = theReel.find_element(By.XPATH, './div[1]/div[3]/div/div/div[1]/div/div/div/div/div')
User = upperInfos.find_element(By.XPATH, './div[1]').text
hover = ActionChains(self.driver).move_to_element(upperInfos.find_element(By.XPATH, './div[2]/span/span/span/span[4]'))
for i in range(50):
hover.perform()
theTime = self.driver.find_element(By.XPATH, '//div[@class="__fb-dark-mode"]//span').text
theTime = datetime.strptime(theTime, '%A, %d %B %Y pada %H.%M')
try:
# If it's nested
theReel.find_element(By.XPATH, './div[1]/div[2]/div/div/div[2]/span//object[@type = "nested/pressable"]/div[@role = "button"]').click()
except:
pass
# This to get the post
theText = theReel.find_element(By.XPATH, './div[1]/div[2]/div/div/div[2]/span').text
# This to get ammount of like, comment, and share
lowerInfos = self.post.find_element(By.XPATH, './div/div/div/div/div/div/div/div/div/div/div[13]/div/div/div[3]/div/div/div/div/div[1]/div')
like = lowerInfos.find_element(By.XPATH, './div[1]/div/span/div/span').text if lowerInfos.find_element(By.XPATH, './div[1]/div/span/div/span').text != '' else 0
comment = lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text != '' else 0
share = lowerInfos.find_element(By.XPATH, './div[2]/div[3]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[3]').text != '' else 0
if theTime in self.theDict['Time'] and theText in self.theDict['Post']:
return None
self.theDict['User'].append(User)
self.theDict['Time'].append(f'{theTime}')
self.theDict['Likes'].append(like)
self.theDict['Comments'].append(comment)
self.theDict['Shares'].append(share)
self.theDict['Post'].append(theText)
def articleHandling(self):
theArticle = self.post.find_element(By.XPATH, './div/div/div/div/div/div/div/div/div/div/div[13]/div/div')
# Get upperInfos (User and Time)
upperInfos = theArticle.find_element(By.XPATH, './div[2]/div/div[2]/div')
try:
# Individual account
User = upperInfos.find_element(By.XPATH, './div[1]//strong[1]').text
hover = ActionChains(self.driver).move_to_element(upperInfos.find_element(By.XPATH, './div[2]/span/div/span[1]'))
for i in range(50):
hover.perform()
theTime = self.driver.find_element(By.XPATH, '//div[@class="__fb-dark-mode"]//span').text
theTime = datetime.strptime(theTime, '%A, %d %B %Y pada %H.%M')
except:
# If it's from a group
groupName = upperInfos.find_element(By.XPATH, './div[1]/span').text
User = upperInfos.find_element(By.XPATH, './div[2]/span/div/span[1]').text
User = f'{groupName}; {User}'
hover = ActionChains(self.driver).move_to_element(upperInfos.find_element(By.XPATH, './div[2]/span/div/span[3]'))
for i in range(50):
hover.perform()
theTime = self.driver.find_element(By.XPATH, '//div[@class="__fb-dark-mode"]//span').text
theTime = datetime.strptime(theTime, '%A, %d %B %Y pada %H.%M')
# Get lowerInfos (Like, Comment, Share)
lowerInfos = theArticle.find_element(By.XPATH, './div[4]/div/div/div/div/div[1]/div')
like = lowerInfos.find_element(By.XPATH, './div[1]/div/span/div/span').text if lowerInfos.find_element(By.XPATH, './div[1]/div/span/div/span').text != '' else 0
# This will check if there's no comment and share
if lowerInfos.find_element(By.XPATH, './div[2]').text == '':
comment = 0
share = 0
else:
try:
# Check if it's not a live stream, what the literal fuck? Why do people use this dogshit feature for this specific tag
lowerInfos.find_element(By.XPATH, './div[2]/div[3]')
comment = lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text != '' else 0
share = lowerInfos.find_element(By.XPATH, './div[2]/div[3]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[3]').text != '' else 0
except:
comment = lowerInfos.find_element(By.XPATH, './div[2]/div[1]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[1]').text != '' else 0
share = lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text.split(" ")[0] if lowerInfos.find_element(By.XPATH, './div[2]/div[2]').text != '' else 0
try:
# If it's nested
theArticle.find_element(By.XPATH, './div[3]/div[1]/div/div/div/div/span//div[@role="button"]').click()
except:
pass
# This to get the post
theText = theArticle.find_element(By.XPATH, './div[3]/div[1]/div/div').text
if theTime in self.theDict['Time'] and theText in self.theDict['Post']:
return None
self.theDict['User'].append(User)
self.theDict['Time'].append(f'{theTime}')
self.theDict['Likes'].append(like)
self.theDict['Comments'].append(comment)
self.theDict['Shares'].append(share)
self.theDict['Post'].append(theText)
def startScrape(self, search: str, startDate: str = "", endDate: str = "", recent: bool = True, savepoint = False, customDayList: List["str"] = None):
'''
PARAMS
- search: str - The search query
- startDate: str - The start date of the search
`YYYY-MM-DD`
- endDate: str - The end date of the search
`YYYY-MM-DD`
- recent: bool - If it's True, it will only scrape the recent post
- savepoint: bool - If it's True, it will save the progress every day
- customDayList: List[str] - If you want to scrape specific date, you can input the list of the date
'''
# Error handling, check parameters
if startDate == "" and endDate == "" and customDayList == None:
raise ValueError("Please input the start date and end date or custom day list")
if startDate != "" and endDate != "" and customDayList != None or startDate != "" and endDate == "" and customDayList != None or startDate == "" and endDate != "" and customDayList != None:
raise ValueError("Please input either start date and end date or custom day list")
if startDate != "" and endDate == "" and customDayList == None:
raise ValueError("Please input the end date")
if startDate == "" and endDate != "" and customDayList == None:
raise ValueError("Please input the start date")
if startDate != "" and endDate != "" and customDayList == None:
dayLists = generateDailyBackwards(startDate, endDate)
elif startDate == "" and endDate == "" and customDayList != None:
dayLists = customDayList
with tqdm(dayLists, ncols=100) as pbar:
for day in pbar:
self.theDict = {"User": [], "Post": [], "Time": [],
"Likes": [], "Comments": [], "Shares": [] }
pbar.set_description(f"Current Date: {day}")
self.searchQuery(search, day, day, recent)
index = 0
reachesEnd = False
while True:
# Break the loop if it reaches the end
if reachesEnd:
if savepoint:
self.savepoint(day)
self.theEntireDataset = pd.concat([self.theEntireDataset, pd.DataFrame(self.theDict)])
break
# This will check if there's a post on this specific date.
try:
feed = self.driver.find_element(By.XPATH, '//div[@role="feed"]')
posts = WebDriverWait(feed, 3).until(EC.presence_of_all_elements_located((By.XPATH, "./div")))
except:
self.theEntireDataset = pd.concat([self.theEntireDataset, pd.DataFrame(self.theDict)])
self.savepoint(day)
break
for i in range(index, len(posts)):
try:
# This is necessary, we need to know how many posts are there every loop. If not found, break and reaches the end
feed = self.driver.find_element(By.XPATH, '//div[@role="feed"]')
posts = WebDriverWait(feed, 3).until(EC.presence_of_all_elements_located((By.XPATH, "./div")))
print(f"Current Index: {index}/{len(posts)}")
# Get the post
if customDayList != None:
self.post = posts[i]
else:
self.post = posts[i+1]
except:
reachesEnd = True
break
# Scroll to the post
self.driver.execute_script("arguments[0].scrollIntoView();", self.post)
self.driver.execute_script("window.scrollBy(0, -100);")
time.sleep(5)
#Just gonna ignore if it's and ad or the post is error or something
try:
# Check whether it's a article or a reel.
try:
self.reelHandling()
except:
# If it's an article
self.articleHandling()
except:
pass
index+=1
# This to adress an issue, where sometimes for loop doesn't break when it reaches the end
if index == len(posts):
reachesEnd = True
break
self.theEntireDataset.to_csv(f'{search}_{startDate}_{endDate}.csv', index=False)
def savepoint(self,currentDate):
'''
This will save the current progress
'''
df = pd.DataFrame(self.theDict)
os.makedirs('savepoints', exist_ok=True)
df.to_csv(f'savepoints/{currentDate}.csv', index=False)
if __name__ == "__main__":
session = facebookScraper()
session.startAndLogin()
session.startScrape('terimakasihjokowi', '2024-1-1', '2024-10-29', recent=True, savepoint=True)