-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.py
93 lines (71 loc) · 2.86 KB
/
experiments.py
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
"""
Using this file as a playground to test new functionality
without running the whole bot.
"""
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import StaleElementReferenceException
from os import path, makedirs
from time import sleep
opt = ChromeOptions()
opt.add_experimental_option('w3c', False)
driver = Chrome(chrome_options=opt)
driver.get('https://www.apple.com')
sleep(2)
print("and:")
print(driver.get_log('browser'))
username, password = open('ig.credentials.txt').read().split(',')
user = {'username': username, 'password': password}
def open_instagram_and_login():
""" Opens instagram.com in Chrome and logs you in using given credentials """
login_route = "https://www.instagram.com/accounts/login/?source=auth_switcher"
# Open Instragram
driver.get(login_route)
wait = WebDriverWait(driver, 10)
wait.until(lambda d: d.current_url == login_route)
# Input Login Credentials
username_input = driver.find_element_by_name("username")
password_input = driver.find_element_by_name("password")
username_input.send_keys(user['username'])
password_input.send_keys(user['password'])
# Login
driver.find_element_by_css_selector("button[type=submit]").click()
sleep(2)
def scroll():
# Go to profile page
desired_url = f"https://www.instagram.com/{user['username']}/"
driver.get(desired_url)
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.current_url == desired_url)
# Click anchor with href="/username/following/"
anchor_href = f"/{user['username']}/following/"
link = driver.find_element_by_css_selector(
f'a[href="{anchor_href}"]')
num_following = int(link.find_element_by_tag_name('span').text)
link.click()
sleep(1)
list_container = driver.find_element_by_css_selector(
'div[role="dialog"] div.isgrP ul div.PZuss')
sleep(1) # Wait for content to load
list_items = list_container.find_elements_by_tag_name('li')
while len(list_items) < num_following - 1:
try:
driver.execute_script(
'arguments[0].scrollIntoView()', list_items[-1])
except StaleElementReferenceException:
sleep(0.5)
list_items = list_container.find_elements_by_tag_name('li')
for item in list_items:
anchors = item.find_elements_by_tag_name('a')
print(
f"Anchors in item: {len(anchors)}. Text: {', '.join([a.text for a in anchors])}")
def detect_duplicates(lst):
duplicates = {}
for item in lst:
if item not in duplicates:
duplicates[item] = -1
duplicates[item] += 1
return duplicates