fb_comment.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from selenium.webdriver.common.keys import Keys
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.chrome.service import Service
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.support import expected_conditions as EC
  7. import time
  8. import redis
  9. import json
  10. import numpy as np
  11. account = ['enjoylisteningswift@yandex.ru']
  12. pd = ['']
  13. path = '/Users/zooeytsai/Downloads/chromedriver 2'
  14. post_url = ['']
  15. def send_comment(text):
  16. s = Service(path)
  17. driver = webdriver.Chrome(service=s)
  18. driver.get('https://www.facebook.com/')
  19. time.sleep(5)
  20. a = driver.find_element(By.ID,"email")
  21. p = driver.find_element(By.ID,"pass")
  22. a.send_keys(account[0])
  23. p.send_keys(pd[0])
  24. time.sleep(3)
  25. login = driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/div[1]/div/div/div/div[2]/div/div[1]/form/div[2]/button')
  26. login.click()
  27. time.sleep(10)
  28. driver.get(post_url[0])
  29. time.sleep(5)
  30. # comment = driver.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[1]/div[5]/div/div[2]/div[2]/div[1]/form/div/div/div[1]/p').send_keys(text)
  31. comment = driver.find_element(By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div/div[2]/div/div/div/div[1]/div[5]/div/div/div[2]/div[1]/form/div/div/div[1]/p')
  32. comment.send_keys(text)
  33. driver.implicitly_wait(5)
  34. comment.send_keys(Keys.ENTER)
  35. driver.implicitly_wait(5)
  36. driver.quit()
  37. def random_comment():
  38. r = redis.Redis(host='db.ptt.cx', port=6379, db=0, password='choozmo9')
  39. js = r.get('yt_comment')
  40. messages = json.loads(js)
  41. # ===============================
  42. r = np.random.randint(0, len(messages))
  43. print(messages[r])
  44. return messages[r]
  45. if __name__ == "__main__":
  46. send_comment(random_comment())