-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path인스타_DM_보내기.py
127 lines (108 loc) · 4.68 KB
/
인스타_DM_보내기.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
121
122
123
124
125
126
127
from logging import error
from selenium import webdriver
from selenium.webdriver.common.by import By
import re, time, random, clipboard
from pprint import pprint
from selenium.webdriver.common.keys import Keys
# pip install webdriver-manager
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
# 필요한 정보 입력 요청 (인스타 ID / PW / 태크 / 파일 위치 및 파일명 지장)
# 파일 저장 위치는 D:\\ 권장 # C: 는 용량 부족
# DM을 보내는 프로그램입니다. DM아이디.txt와 DM메시지.txt를 수정해주세요.
instaID = '' #아이디입니다.
instaPW = '' #비번입니다.
# webdriver 객체 생성 # 크롬 버전과 동일한 chromedriver설치
driver = webdriver.Chrome(executable_path='C:\workspace/chromedriver.exe')
driver.implicitly_wait(5)
# 웹 사이트 접속 (인스타그램 로그인)
driver.get('https://www.instagram.com/accounts/login/')
# 로그인 정보 입력
clipboard.copy(instaID) # 복사하기
instaID = clipboard.paste()
id_dr = driver.find_element_by_name('username')
id_dr.send_keys(Keys.CONTROL,'v') #붙여넣기
clipboard.copy(instaPW) # 복사하기
instaPW = clipboard.paste()
ps_dr = driver.find_element_by_name('password')
ps_dr.send_keys(Keys.CONTROL,'v')#붙여넣기
clipboard.copy('.')# 비번없에기
driver.find_element_by_xpath(
'//*[@id="loginForm"]/div/div[3]/button').submit()
time.sleep(random.uniform(3,6))
# DM보낼 아이디 열기
f = open("C:/workspace/DM아이디.txt", 'r')
string = f.read()
# DM메시지 열기
fcontent = open("C:/workspace/DM게시글.txt", 'r', encoding='UTF8')
strcontent = fcontent.read()
# a = string.split(',') # split함수를 사용해 ',' 기준으로 나눕니다.
a = string.split('\n') # split함수를 사용해 '\n' 기준으로 나눕니다.
# print(a)
i = 0
b = len(a)
print(b)
# DM보낼 아이디 수 만큼 반복
string = str(string)
while i < b:
try:
wID = a[i]
iint = i + 1
iint = str(iint)
print(iint)
wID = str(wID)
print(wID)
d = re.search((wID), string)
# 웹 사이트 접속 (해시태그 검색)
time.sleep(random.uniform(2,3))
driver.get('https://www.instagram.com/' + wID + '/')
time.sleep(1)
i += 1
except Exception as e:
d = 'error'
i = 'error'
print('반복 error > ', e)
break
# 팔로우&메시지 보내기 버튼 클릭
try:
driver.find_element(By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/div[1]/div[1]/div/div/button').click()
time.sleep(1)
print('팔로우&메시지 보내기 버튼을 클릭했습니다.')
except Exception as e:
print('클릭 error > ', e)
break
# # 팔로우가 되어 있지 않으면 메시지 보내기 클릭
try:
driver.find_element_by_css_selector('#react-root > section > main > div > header > section > div.nZSzR > div.qF0y9.Igw0E.IwRSH.eGOV_.ybXk5._4EzTm.bPdm3 > div > div > button').click()
time.sleep(1)
print('메시지 보내기를 클릭했습니다.')
except:
print('메시지 보내기 버튼이 이미 클릭이 되었습니다.')
#팝업창 닫기
try:
driver.find_element_by_css_selector('body > div.RnEpo.Yx5HN > div > div > div > div.mt3GC > button.aOOlW.HoLwm').click()
time.sleep(1)
print('다음에하기 누르기')
except:
print('다음에하기 누르기가 없습니다. ')
#DM메시지 넣기
try: #send_key()메소드 매개변수는 이모티콘이 포함된 문장있으면 에러
clipboard.copy(strcontent) # 복사하기
strcontent = clipboard.paste()
dm = driver.find_element_by_xpath('//*[@id="react-root"]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea')
dm.send_keys(Keys.CONTROL,'v') #붙여넣기
print('DM 내용넣기')
pprint(strcontent)
except Exception as e:
print('DM메시지 넣기 error > ', e)
break
#DM메시지 보네기
try:
driver.find_element_by_css_selector('#react-root > section > div > div.qF0y9.Igw0E.IwRSH.eGOV_._4EzTm > div > div > div.DPiy6.qF0y9.Igw0E.IwRSH.eGOV_.vwCYk > div.uueGX > div > div.qF0y9.Igw0E.IwRSH.eGOV_._4EzTm > div > div > div.qF0y9.Igw0E.IwRSH.eGOV_._4EzTm.JI_ht > button').click()
print('DM 내용보내기')
except Exception as e:
print('DM메시지 보네기 error > ', e)
break
print("DM 보내기가 끝났습니다.")
f.close()
fcontent.close()