-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotInstagram3.py
More file actions
69 lines (62 loc) · 2.68 KB
/
botInstagram3.py
File metadata and controls
69 lines (62 loc) · 2.68 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
# Bot para comentar em fotos de uma tag específica do Instagram
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
class InstagramBot3:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Chrome(executable_path=r'C:/Users/rayan/Desktop/Cursos Quarentena/Bots - Python/chromedriver.exe')
def login(self):
driver = self.driver
driver.get('https://www.instagram.com')
time.sleep(2)
user_element = driver.find_element_by_xpath("//input[@name='username']")
user_element.clear()
user_element.send_keys(self.username)
password_element = driver.find_element_by_xpath("//input[@name='password']")
password_element.clear()
password_element.send_keys(self.password)
password_element.send_keys(Keys.RETURN)
time.sleep(5)
self.comentarFotosdaTag('sorteio')
@staticmethod
def digitarComoHumano(frase, onde_digitar):
for letra in frase:
onde_digitar.send_keys(letra)
time.sleep(random.randint(1,5)/30)
def comentarFotosdaTag(self, hashtag):
links_de_posts = []
driver = self.driver
driver.get('https://www.instagram.com/explore/tags/' + hashtag + '/')
time.sleep(5)
for i in range(1,3):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
hrefs = driver.find_elements_by_tag_name('a')
pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
print(hashtag + " fotos: " + str(len(pic_hrefs)))
for link in pic_hrefs:
try:
if link.index("/p/") != -1:
links_de_posts.append(link)
except:
pass
for pic_href in links_de_posts:
driver.get(pic_href)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
comentarios = ['1', '2', '3']
driver.find_element_by_class_name("Ypffh").click()
campo_comentario = driver.find_element_by_class_name("Ypffh")
time.sleep(5)
self.digitarComoHumano(random.choice(comentarios), campo_comentario)
time.sleep(random.randint(30,120))
driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()
time.sleep(5)
except Exception as e:
print(e)
time.sleep(5)
testBot = InstagramBot3('','') # TODO: inserir username e senha
testBot.login()