상대의 페이지로 들어간뒤 첫 게시글을 클릭하여 좋아요와 댓글을다는 순서입니다. 코드입니다.
from selenium import webdriver
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtWidgets import QToolButton, QSizePolicy, QLabel, QLineEdit,QPushButton
import random
import keyboard
import re
options=webdriver.ChromeOptions()
options.add_argument('headless')#창을 안띄우는 headless모드
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")#gpu가속 끔
#user-agent값을 변경하여 headless모드 감지를 방지
options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
options.add_argument("lang=ko_KR") #headless모드에선 언어설정이 안되있으므로 한국어로 설정(감지 방지)
driver = webdriver.Chrome('C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe',chrome_options=options)
class start(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout=QtWidgets.QHBoxLayout(self)
self.id_layout = QtWidgets.QVBoxLayout(self)
self.pwd_layout = QtWidgets.QVBoxLayout(self) #QV=가로 QH=세로
self.id=QLabel("ID")
self._id=QLineEdit()
self.pwd=QLabel("password")
self._pwd=QLineEdit()
self.id_layout.addWidget(self.id)
self.id_layout.addWidget(self._id)
self.pwd_layout.addWidget(self.pwd)
self.pwd_layout.addWidget(self._pwd)
self.layout.addLayout(self.id_layout)
self.layout.addLayout(self.pwd_layout)
self.setLayout(self.layout)
self._pwd.setEchoMode(QLineEdit.Password)
self._pwd.returnPressed.connect(self.login)
self.show()
def login(self):
driver.get("https://www.instagram.com/accounts/login/")
driver.implicitly_wait(10)
driver.find_element_by_name('username').send_keys(self._id.text())
driver.find_element_by_name('password').send_keys(self._pwd.text())
driver.implicitly_wait(10)
driver.find_elements_by_tag_name('button')[1].click()
driver.implicitly_wait(100)
self.newWindow = Main()
self.newWindow.show()
self.close()
class Main(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout_m = QtWidgets.QHBoxLayout(self) #전체틀
self.start_layout = QtWidgets.QVBoxLayout() #버튼을 담을 틀
self.setFixedSize(300, 200)
self.target=QLabel("상대의 아이디를 입력하세요")
self._target=QLineEdit()
self._target.returnPressed.connect(self.targetID)
self.like = self.createButton("좋아요",self.clicklike)
self.like.resize(self.like.sizeHint())
self.start_layout.addWidget(self.target)
self.start_layout.addWidget(self._target)
self.layout_m.addLayout(self.start_layout)#전체틀에 버튼을 담은 틀을 담음
self.layout_m.addWidget(self.like)
self.setLayout(self.layout_m)
self.show()
def createButton(self, text, function):
button = Button(text)
button.clicked.connect(function)
return button
def targetID(self):
self.target_name=self._target.text()
driver.get('https://www.instagram.com/{}'.format(self.target_name))
def clicklike(self):
#첫번째 게시글 클릭
driver.find_elements_by_css_selector('.v1Nh3.kIKUG._bz0w')[0].find_element_by_tag_name('a').click()
driver.implicitly_wait(10)
#좋아요누르기
driver.get(driver.current_url)
temp1=driver.find_elements_by_class_name('eo2As ')
temp1[0].find_element_by_class_name('wpO6b ').click()
#사진의 정보가져와서 필요한데이터 추출
img_information=driver.find_elements_by_tag_name('img')[1].get_attribute('alt')
img_information=img_information.split(': ')
img_information=img_information[1].split(' and ')
food = ["맛있겠네","맛있어 보이는구나!","다음에 나도 데려가~~", "돼지야!"]
people=["오 아주 잘나왔군!", "정말 멋쟁이군", "우도환 닮았다", "손나은 닮았다"]
for i in img_information:
if re.search('people',i):
case="인물"
break
if re.search('food',i):
case="음식"
break
else:
case="기타"
if case=="인물":
rand_number=random.randint(0,3)
comment=people[rand_number]
elif case=="음식":
rand_number=random.randint(0,3)
comment=food[rand_number]
else:
comment="^^7"
#댓글입력
temp1[0].find_elements_by_class_name('wpO6b ')[1].click()
driver.find_element_by_tag_name('textarea').send_keys(comment)
driver.implicitly_wait(10)
driver.find_element_by_class_name('X7cDz').find_element_by_tag_name('button').click()