I have written some code in python in combination with selenium to parse all the names from facebook friend list. It was hard to manage the pop up notification and the process of scrolling to the end of that page. However, my scraper can do that successfully. I tried to do the whole thing very carefully. There are always rooms for improvement, though. here is the working code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
def Facebook_Crawler(driver):
driver.get('https://www.facebook.com/login.php?login_attempt=1&lwv=111')
driver.find_element_by_xpath('//*[@id="email"]').send_keys('SomeEmail')
driver.find_element_by_xpath('//*[@id="pass"]').send_keys('Password')
driver.find_element_by_xpath('//*[@id="loginbutton"]').click()
time.sleep(5)
driver.find_element_by_xpath("//a[@id='findFriendsNav']").click()
time.sleep(5)
num_Of_links = -1
num = 0
while num != num_Of_links:
num_Of_links = num
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(8)
items = [post.text for post in driver.find_elements_by_xpath("//div[contains(@class,'friendBrowserNameTitle')]/a")]
num = len(items)
for item in items:
print(item)
if __name__ == '__main__':
driver = webdriver.Chrome(chrome_options = chrome_options)
try:
Facebook_Crawler(driver)
finally:
driver.quit()