Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. from selenium import webdriver as driver # fakes human interaction
  2. from selenium.webdriver.common.keys import Keys as keys
  3. import time # waiting
  4.  
  5. #consts
  6. ENTRY_POINT="https://www.facebook.com"
  7. USERNAME="TheDesignerPenguin@gmail.com"
  8. PASSWORD="LarissaLee"
  9. LOGIN_FAILED_URL="http://www.facebook.com/login.php"
  10. TIMELINE_FRIEND_CONFIRM="Friends" # Look for this text to find the friends button
  11. SCROLL_LOAD_TIME=1
  12.  
  13. #selector consts
  14. sBACKGROUND="#mainContainer"
  15. sLOGIN_CSS="#email"
  16. sPASSWORD_CSS="#pass"
  17. sLOG_IN_CSS="#loginbutton input"
  18. sPROFILE="._2s25"
  19. sTIMELINE_BUTTONS="._6-6"
  20. sFRIEND_COUNT="div a span._gs6"
  21. sPERSON_NAME="#fb-timeline-cover-name"
  22. sSCROLL_FINISHED=".uiHeaderTitle"
  23.  
  24. #opens the browser at a url
  25. def create(url):
  26.     browser = driver.Chrome()
  27.     browser.get(url)
  28.  
  29.     return browser
  30.  
  31. def go_to(browser, url):
  32.     browser.get(url)
  33.  
  34. def get_url(browser):
  35.     return browser.current_url
  36.  
  37. def find_element(browser, css, ty='css'):
  38.     return browser.find_element(ty, css)
  39.  
  40. def find_elements(browser, css, ty='css'):
  41.     return browser.find_elements(ty, css)
  42.  
  43. def enter_data(element, data):
  44.     element.send_keys(data)
  45.  
  46. def submit_data(element):
  47.     element.submit()
  48.  
  49. def click(element):
  50.     element.click()
  51.  
  52. def close(browser, code=0):
  53.     browser.quit()
  54.     print("quitting")
  55.     exit(code)
  56.  
  57. #open up the browser to facebook.com
  58. browser = create(ENTRY_POINT)
  59.  
  60. #log into account
  61. enter_data(find_element(browser, sLOGIN_CSS), USERNAME)
  62. enter_data(find_element(browser, sPASSWORD_CSS), PASSWORD)
  63. click(find_element(browser, sLOG_IN_CSS))
  64.  
  65. #if the account login has failed, the url will be fb.com/login
  66. if LOGIN_FAILED_URL in get_url(browser):
  67.     print("Login failed")
  68.     close(browser)
  69.  
  70. print("login successful")
  71.  
  72. #Goto the profile
  73. go_to(browser, find_element(browser, sPROFILE).get_attribute('href'))
  74.  
  75. #Because of the way facebook is designed there is no unique ID for the friends button
  76. #Instead it is known to be the 2nd button in the list
  77. #Bad code
  78. profile_link=None
  79.  
  80. for l in find_elements(browser, sTIMELINE_BUTTONS):
  81.     if TIMELINE_FRIEND_CONFIRM in l.text:
  82.         profile_link=l
  83.  
  84. if profile_link == None:
  85.     print("Profile could not be found")
  86.     close(browser)
  87.        
  88. go_to(browser, profile_link.get_attribute('href'))
  89.  
  90. print("navigated to friends")
  91.  
  92. #if the login was successful, fucking facebook will ask for notifications
  93. #click on the background to remove
  94. enter_data(find_element(browser, sPROFILE), keys.ESCAPE)
  95.  
  96. #grab data about the person being scanned
  97. person_name=find_element(browser, sPERSON_NAME).text
  98.  
  99. #the friends element is now selected, thus we can grab that and get the child
  100. #this child shows the number of friends
  101. person_friend_count_raw=find_element(browser, sFRIEND_COUNT).text
  102.  
  103. #friend count contains the phrase 'Mutual'
  104. #remove it
  105. person_friend_count=""
  106.  
  107. for char in person_friend_count_raw:
  108.     if char.isdigit():
  109.         person_friend_count += char
  110.  
  111. print("collected personal information:")
  112. print("{} has {} friend".format(person_name, person_friend_count))
  113.  
  114. # now the bot needs to collect an array of all the friends elements
  115.  
  116. # first scroll to the bottom of the page
  117. for i in range(10):
  118.  
  119.     #scroll down
  120.     browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  121.     time.sleep(SCROLL_LOAD_TIME)
  122.  
  123.     #check if the done scrolling element is visible
  124.     #slow, and I have try + except
  125.     try:
  126.         find_element(#im not done son
  127.  
  128. time.sleep(3)
  129. close(browser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement