Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver as driver # fakes human interaction
- from selenium.webdriver.common.keys import Keys as keys
- import time # waiting
- #consts
- ENTRY_POINT="https://www.facebook.com"
- USERNAME="TheDesignerPenguin@gmail.com"
- PASSWORD="LarissaLee"
- LOGIN_FAILED_URL="http://www.facebook.com/login.php"
- TIMELINE_FRIEND_CONFIRM="Friends" # Look for this text to find the friends button
- SCROLL_LOAD_TIME=1
- #selector consts
- sBACKGROUND="#mainContainer"
- sLOGIN_CSS="#email"
- sPASSWORD_CSS="#pass"
- sLOG_IN_CSS="#loginbutton input"
- sPROFILE="._2s25"
- sTIMELINE_BUTTONS="._6-6"
- sFRIEND_COUNT="div a span._gs6"
- sPERSON_NAME="#fb-timeline-cover-name"
- sSCROLL_FINISHED=".uiHeaderTitle"
- #opens the browser at a url
- def create(url):
- browser = driver.Chrome()
- browser.get(url)
- return browser
- def go_to(browser, url):
- browser.get(url)
- def get_url(browser):
- return browser.current_url
- def find_element(browser, css, ty='css'):
- return browser.find_element(ty, css)
- def find_elements(browser, css, ty='css'):
- return browser.find_elements(ty, css)
- def enter_data(element, data):
- element.send_keys(data)
- def submit_data(element):
- element.submit()
- def click(element):
- element.click()
- def close(browser, code=0):
- browser.quit()
- print("quitting")
- exit(code)
- #open up the browser to facebook.com
- browser = create(ENTRY_POINT)
- #log into account
- enter_data(find_element(browser, sLOGIN_CSS), USERNAME)
- enter_data(find_element(browser, sPASSWORD_CSS), PASSWORD)
- click(find_element(browser, sLOG_IN_CSS))
- #if the account login has failed, the url will be fb.com/login
- if LOGIN_FAILED_URL in get_url(browser):
- print("Login failed")
- close(browser)
- print("login successful")
- #Goto the profile
- go_to(browser, find_element(browser, sPROFILE).get_attribute('href'))
- #Because of the way facebook is designed there is no unique ID for the friends button
- #Instead it is known to be the 2nd button in the list
- #Bad code
- profile_link=None
- for l in find_elements(browser, sTIMELINE_BUTTONS):
- if TIMELINE_FRIEND_CONFIRM in l.text:
- profile_link=l
- if profile_link == None:
- print("Profile could not be found")
- close(browser)
- go_to(browser, profile_link.get_attribute('href'))
- print("navigated to friends")
- #if the login was successful, fucking facebook will ask for notifications
- #click on the background to remove
- enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
- #grab data about the person being scanned
- person_name=find_element(browser, sPERSON_NAME).text
- #the friends element is now selected, thus we can grab that and get the child
- #this child shows the number of friends
- person_friend_count_raw=find_element(browser, sFRIEND_COUNT).text
- #friend count contains the phrase 'Mutual'
- #remove it
- person_friend_count=""
- for char in person_friend_count_raw:
- if char.isdigit():
- person_friend_count += char
- print("collected personal information:")
- print("{} has {} friend".format(person_name, person_friend_count))
- # now the bot needs to collect an array of all the friends elements
- # first scroll to the bottom of the page
- for i in range(10):
- #scroll down
- browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
- time.sleep(SCROLL_LOAD_TIME)
- #check if the done scrolling element is visible
- #slow, and I have try + except
- try:
- find_element(#im not done son
- time.sleep(3)
- close(browser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement