-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
120 lines (95 loc) · 3.43 KB
/
run.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
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
119
120
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.by import By
import time
CONFIGS = {
'USERNAME': 'YOUR_USERNAME',
'PASSWORD': 'YOUR_PASSWORD',
'HASHTAGS': [
'FIRST_HASHTAG', 'SECOND_HASHTAG'
],
'TOTAL_LIKES_PER_HASHTAG': 300,
'SYSTEM': 'windows/linux/mac'
}
total_likes = 0
driver = webdriver.Chrome('./chromedriver_{}'.format(CONFIGS['SYSTEM']))
print("Loading Instagram")
driver.get("https://www.instagram.com/")
print("Logging in")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.NAME, 'username'
)))
usernameInput = driver.find_element(By.NAME, 'username')
usernameInput.send_keys(CONFIGS['USERNAME'])
passwordInput = driver.find_element(By.NAME, 'password')
passwordInput.send_keys(CONFIGS['PASSWORD'])
passwordInput.submit()
# Search
WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.XPATH, "/html/body/div[1]/section/nav/div[2]/div/div/div[2]/input"
)))
print("Logged in!")
print("Navigating through hashtags...")
for current_hashtag in CONFIGS['HASHTAGS']:
driver.get(
"https://www.instagram.com/explore/tags/{}/".format(current_hashtag)
)
try:
# Search results
WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.TAG_NAME, "h1"
)))
except:
print("Hashtag #{} not loaded, going to next...".format(current_hashtag))
continue
print("Loaded #{}".format(current_hashtag))
print("Checking for results...")
results = driver.find_elements(By.XPATH, "//a[contains(@href, '/p/')]")
if not results:
print("No results for #{}".format(current_hashtag))
driver.quit()
print("Opening first post from #{}...".format(current_hashtag))
results[0].click()
current_hashtag_likes = 0
while current_hashtag_likes < CONFIGS['TOTAL_LIKES_PER_HASHTAG']:
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.TAG_NAME, "time"
)))
except:
print("Something is wrong in URL: {}".format(driver.current_url))
driver.quit()
time.sleep(1)
try:
likeButton = driver.find_element(
By.XPATH, "/html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button"
)
likeButton.click()
print("[#{}/{}/{}] Liked: {}".format(
current_hashtag,
current_hashtag_likes,
CONFIGS['TOTAL_LIKES_PER_HASHTAG'],
driver.current_url
))
total_likes = total_likes + 1
current_hashtag_likes = current_hashtag_likes + 1
time.sleep(10)
except Exception as e:
print(e)
pass
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.CLASS_NAME, 'coreSpriteRightPaginationArrow'
)))
nextButton = driver.find_element(
By.CLASS_NAME, 'coreSpriteRightPaginationArrow'
)
nextButton.click()
except:
print("Something wrong, going to next hashtag...")
break
print("Exiting...")
print("=====================")
print("Total posts liked: {}".format(total_likes))
driver.quit()