-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlike_data.py
More file actions
118 lines (93 loc) · 4.83 KB
/
Copy pathlike_data.py
File metadata and controls
118 lines (93 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import time
import re
import pandas as pd
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
class MyLikes:
def __init__(self, url, driver_path, records_path) -> None:
self.url = url
self.URLS = list()
self.profile_urls = list()
self.records = list()
self.picture_count = 0
self.records_path = records_path
self.name_regEx = re.compile(pattern=r'itemprop=\"name\">(.+)(?=</[sS]pan></[dD]iv><[sS]pan [cC]lass=)')
self.age_regEx = re.compile(pattern=r'=\"age\">(\d*)<')
self.url_regEx = re.compile(pattern=r'url\(\"(https://.+\.jpg)\"')
self.options = webdriver.ChromeOptions()
self.options.add_experimental_option('debuggerAddress', 'localhost:9222')
self.driver = webdriver.Chrome(executable_path=driver_path, options=self.options)
def __repr__(self) -> str:
return 'This script gets the data from your likes on Tinder. You must have (1) Tinder Platinum and (2) Chrome running on a localhost.'
def main(self):
self.driver.get(url=self.url)
# Click the Allow button
try:
self.driver.find_element_by_xpath(xpath='//*[@id="c207610411"]/div/div/div/div/div[3]/button[1]/span').click()
except Exception as e:
pass
time.sleep(2)
# Click the I Accept button
try:
self.driver.find_element_by_xpath(xpath='//*[@id="c-13730025"]/div/div[2]/div/div/div[1]/button').click()
except Exception as e:
pass
time.sleep(2)
# Click the Likes Sent button
self.driver.find_element_by_xpath(xpath='//a[@href="/app/my-likes"]').click() # Selecting by the href of an anchor (i.e., 'a') tag
time.sleep(3)
# Press a point on the page that gives a reference for scrolling to the bottom
self.random_div = self.driver.find_element_by_xpath(xpath='//*[@id="c609262533"]/div[1]/div/button')
self.action = webdriver.common.action_chains.ActionChains(self.driver)
self.action.move_to_element_with_offset(self.random_div, 0, 90)
self.action.click()
self.action.perform()
self.html = self.driver.find_element_by_tag_name('html')
time.sleep(3)
# Scroll to the bottom and gather data for dataframe
for _ in range(130):
final_html = self.driver.page_source
self.soup = BeautifulSoup(final_html, 'html.parser')
cards = self.soup.find_all('div', {'class': 'Bdrs(8px) Bgz(cv) Bgp(c) StretchedBox'})
stats = self.soup.find_all('div', {'class': 'D(f) Ai(c) Miw(0)'})
stat_count = 0
for card in cards:
if 'url' in str(card):
the_url = re.search(pattern=self.url_regEx, string=str(card)).group(1)
if the_url in self.URLS:
stat_count += 1
continue
else:
self.URLS.append(the_url)
self.picture_count += 1
the_stat = str(stats[stat_count])
# Get the person's name
self.name = re.search(pattern=self.name_regEx, string=the_stat).group(1).title()
# Try to get the person's age
try:
self.age = re.search(pattern=self.age_regEx, string=the_stat).group(1)
except Exception as e:
self.age = 'Age Not Found'
# Append person's name and age to the "records" list and print to stdout
self.records.append((self.name, self.age))
print(f'Name: {self.name}, Age: {self.age}')
# Download the profile picture
r = requests.get(url=the_url)
with open(file=f'./tinder_pics/{self.picture_count}_{self.name}.jpg', mode='wb') as pp:
pp.write(r.content)
# Increment the count for the section that has the person's name and age
stat_count += 1
time.sleep(1.5)
self.html.send_keys(Keys.END)
def pandas_to_excel(self):
self.df = pd.DataFrame(data=self.records, columns=['Name', 'Age'])
self.df.to_excel(excel_writer=self.records_path, sheet_name='Tinder Results', index=False, freeze_panes=(1,0))
# Instantiate the class and call the necessary functions, inputting your arguments
if __name__ == '__main__':
c = MyLikes(url='https://tinder.com/app/recs',
driver_path=r'C:\Users\Alex\Desktop\Python drivers\chromedriver.exe',
records_path=r'C:\Users\Alex\Desktop\Tinder_data.xlsx')
c.main()
c.pandas_to_excel()