Skip to content

Instantly share code, notes, and snippets.

@apjanco
Created May 20, 2020 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apjanco/a11c73662144d0637735338aacf7185b to your computer and use it in GitHub Desktop.
Save apjanco/a11c73662144d0637735338aacf7185b to your computer and use it in GitHub Desktop.
import facebookscroller
import requests
import json
import time
import os
cwd = os.getcwd()
facebook_email = "apjanco@uchicago.edu"
facebook_password = "password"
target_profile = "maria.tselovatova"
# login to Facebook account
facebookscroller.login(facebook_email, facebook_password)
# scroll the account so we can access all friends
facebookscroller.scroll_user_account(target_profile,friends=True)
# grab the Selenium driver variable so we can use it
driver = facebookscroller.driver
# this grabs a content block
friends = driver.find_elements_by_class_name("_698")
friends_list = []
for friend in friends:
row = {}
name = friend.find_elements_by_class_name("fsl")[0]
row['name'] = name.text
row['url'] = name.find_element_by_tag_name("a").get_attribute("href")
profile_image = friend.find_elements_by_class_name("img")[0].get_attribute("src")
row['profile_image'] = profile_image
r = requests.get(profile_image, allow_redirects=True)
#check if user profiles directory, add if not exists and save photos to own directory
if not os.path.isdir(cwd + '/' + target_profile +'__images'):
os.mkdir(cwd + '/' + target_profile +'__images')
open(cwd + '/' + target_profile +'__images/' + name.text +'.jpg', 'wb').write(r.content)
#print(name.text,row['url'], row['profile_image'])
friends_list.append(row)
print("[*] Discovered %d friends for %s" % (len(friends),target_profile))
facebookscroller.save_friends_list(target_profile,friends_list)
# close our browser
driver.quit()
import time
import random
import codecs
import csv
import json
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import sys
import os
cwd = os.getcwd()
#reload(sys)
#sys.setdefaultencoding("utf8")
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(executable_path="/home/ajanco/Documents/chromedriver",chrome_options=chrome_options)
#
# Login to the Facebook account
#
def login(facebook_email,facebook_password):
driver.get("https://www.facebook.com/")
# login to Facebook
elem = driver.find_element_by_id('email')
elem.send_keys(facebook_email)
elem = driver.find_element_by_id('pass')
elem.send_keys(facebook_password)
elem.send_keys(Keys.RETURN)
time.sleep(1)
return
#
# Scroll a user account to load friends, posts, etc.
#
def scroll_user_account(target_profile,friends=False):
# browse to the target's friends list
if friends == False:
profile_url = "https://www.facebook.com/%s" % target_profile
else:
profile_url = "https://www.facebook.com/%s/friends" % target_profile
driver.get(profile_url)
time.sleep(5)
# scroll to the bottom of the page
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
time.sleep(1)
# check number of elements
element_size = len(driver.find_elements_by_xpath("//*"))
friends_count = len(driver.find_elements_by_class_name("_698"))
stop = False
while stop is False:
element_size_start = element_size
friends_count_start = friends_count
# scroll to the bottom again
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
# count the number of elements
element_size = len(driver.find_elements_by_xpath("//*"))
friends_count = len(driver.find_elements_by_class_name("_698"))
time.sleep(1)
# scroll to the top of the page
driver.execute_script("window.scrollTo(0,0);")
# if we have more elements loaded repeat scrolling
if element_size <= element_size_start:
url = 'https://www.facebook.com/' + target_profile + '/photos'
try:
stopper = driver.find_element_by_xpath('//a[@href="'+url+'"]')
stop = True
except:
continue
spinners = driver.find_elements_by_class_name("_359")
# find the loading animation element
if len(spinners):
for spinner_element in spinners:
scroll_into_view(spinner_element)
time.sleep(1)
else:
# we couldn't find any more loading animations so quit
break
return
#
# Helper function to scroll an element into view
#
def scroll_into_view(element):
scroll = ActionChains(driver).move_to_element(element)
scroll.perform()
return
#
# Saves a friends list to CSV
#
def save_friends_list(target_profile,friends_list):
# open a CSV file for writing and dump out the results
headers = ["name","url","profile_image"]
with open("%s-friends.csv" % target_profile,"w") as fd:
spreadsheet = csv.DictWriter(fd,fieldnames=headers)
spreadsheet.writeheader()
for friend in friends_list:
print(friend)
spreadsheet.writerow(friend)
print("[*] Wrote out %d friends for %s" % (len(friends_list),target_profile))
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment