Day 050

AWESOMee·2022년 3월 24일
0

Udemy Python Bootcamp

목록 보기
50/64
post-thumbnail

Udemy Python Bootcamp Day 050

Auto Tinder Swiping Bot

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

service = Service("/Users/****/Development/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("https://tinder.com/app/recs")
time.sleep(5)
log_in_button = driver.find_element(By.XPATH, "//*[text()='Log in']")
log_in_button.click()
time.sleep(5)
log_in_with_facebook = driver.find_element(By.XPATH, "//*[text()='Login with Facebook']")
log_in_with_facebook.click()

페이스북 로그인창 띄우는데 한시간 걸렸어... 이럴일이야..?
log_in_with_facebook에서 그냥 XPATH말고 FULL XPATH하고
driver불러오기전에 time.sleep(5)해줘야 함....

Login with Facebook

EMAIL = "******@gmail.com"
PASSWORD = "******"

base_window = driver.window_handles[0]
fb_login_window = driver.window_handles[1]
driver.switch_to.window(fb_login_window)

facebook_email = driver.find_element(By.ID, "email")
facebook_email.send_keys(EMAIL)
facebook_password = driver.find_element(By.ID, "pass")
facebook_password.send_keys(PASSWORD)
facebook_password.send_keys(Keys.ENTER)


driver.switch_to.window(base_window)

driver.window_handles만 처음 배웠고 나머지는 무난하게함

Dismiss all requests

time.sleep(5)
location_allow_button = driver.find_element(By.XPATH, "//*[text()='Allow']")
location_allow_button.click()
notification_button = driver.find_element(By.XPATH, "//*[text()='Not interested']")
notification_button.click()
cookie_accept_button = driver.find_element(By.XPATH, "//*[text()='I accept']")
cookie_accept_button.click()

여기까지도 잘 함!!

이거때문에 아무것도 못하고 있음... 이거 오늘 끝낼 수 있냐,,,
그나저나 (By.XPATH, "//*[text()='I accept']")이렇게 쓰는거 되게 편하네

Hit Like!

from selenium.common.exceptions import ElementClickInterceptedException

#Tinder free tier only allows 100 "Likes" per day. If you have a premium account, feel free to change to a while loop.
for n in range(100):

    #Add a 1 second delay between likes.
    sleep(1)

    try:
        print("called")
        like_button = driver.find_element(By.XPATH,
            '//*[@id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/div[4]/button')
        like_button.click()

    #Catches the cases where there is a "Matched" pop-up in front of the "Like" button:
    except ElementClickInterceptedException:
        try:
            match_popup = driver.find_element(By.CSS_SELECTOR, ".itsAMatch a")
            match_popup.click()

        #Catches the cases where the "Like" button has not yet loaded, so wait 2 seconds before retrying.
        except NoSuchElementException:
            sleep(2)

driver.quit()

아무래도 계정 차단ㅋㅋㅋㅋㅋㅋ 당한듯...?
내 사진도 아니고 ㅎ 이름도 아니고 ㅎ,,,
오로지 플젝을 위해 만든거라 개인정보 털기 싫었단 말이야...
암튼 그렇기 때문에 그냥 솔루션 가져옴 ㅎㅎ..
아마 like 버튼은 솔루션과 같은 xpath로 쓰는 것보다 span명으로 쓰는게 더 나을 것으로 사료됨
그리고 아마 exception도 에러 났을거같음.. 왜냐면 어제도 그랬기 때문..

+) 2022.03.26
import exception안해서 그런거였답니다..

FINAL

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException

import time

EMAIL = "******@gmail.com"
PASSWORD = "*****"

service = Service("/Users/****/Development/chromedriver")
driver = webdriver.Chrome(service=service)
driver.get("https://tinder.com/app/recs")
time.sleep(5)
log_in_button = driver.find_element(By.XPATH, "//*[text()='Log in']")
log_in_button.click()
time.sleep(3)
log_in_with_facebook = driver.find_element(By.XPATH, "//*[text()='Login with Facebook']")
log_in_with_facebook.click()

base_window = driver.window_handles[0]
fb_login_window = driver.window_handles[1]
driver.switch_to.window(fb_login_window)

facebook_email = driver.find_element(By.ID, "email")
facebook_email.send_keys(EMAIL)
facebook_password = driver.find_element(By.ID, "pass")
facebook_password.send_keys(PASSWORD)
facebook_password.send_keys(Keys.ENTER)

driver.switch_to.window(base_window)
time.sleep(5)
location_allow_button = driver.find_element(By.XPATH, "//*[text()='Allow']")
location_allow_button.click()
notification_button = driver.find_element(By.XPATH, "//*[text()='Not interested']")
notification_button.click()
cookie_accept_button = driver.find_element(By.XPATH, "//*[text()='I accept']")
cookie_accept_button.click()

for n in range(100):
    time.sleep(1)
    try:
        print("called")
        like_button = driver.find_element(By.XPATH,
            '//*[@id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/div[4]/button')
        like_button.click()

    except ElementClickInterceptedException:
        try:
            match_popup = driver.find_element(By.CSS_SELECTOR, ".itsAMatch a")
            match_popup.click()

        except NoSuchElementException:
            time.sleep(2)

driver.quit()

암튼 절반 했다..
이걸 했다고 할 수 있는지는 모르겠지만 ㅋㅋㅋㅋㅋ
50일까지 꾸준히 했다는 거에 의의를 두자고...

profile
개발을 배우는 듯 하면서도

0개의 댓글