본문 바로가기
IT/프로그래밍

파이썬 중고나라, 11번가 봇 만들기 - 내 일을 바꾸는 업무 자동화 10장

by nutrient 2021. 5. 29.
728x90
728x170

 

 

 

 

 

auto_web_11st.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('chromedriver')

try:
    driver.get('http://11st.co.kr')

    elem = driver.find_element_by_class_name('search_text')
    elem.send_keys('자전거')
    elem.send_keys(Keys.RETURN)

    from openpyxl import Workbook
    result_xlsx = Workbook()
    worksheet = result_xlsx.active
    worksheet.append(['상품명', '가격'])

    elems = driver.find_elements_by_xpath("//ul[contains(@class, 'c_listing_view_type_list')]/li")
    for elem in elems:
        title_tag = elem.find_element_by_class_name('c_prd_name')
        # print(title_tag.text)
        price_tag = elem.find_element_by_xpath('.//div[@class="c_prd_price"]//span[@class="value"]')
        print(title_tag.text, price_tag.text)
        worksheet.append([title_tag.text, price_tag.text])

    file_name = 'C:\\python\\examples\\2.6\\11st_result.xlsx'
    result_xlsx.save(file_name)

    from my_email import send_mail
    send_mail('이태화', 'alghost.lee@gmail.com', '테스트', file_name)
except Exception as e:
    print(e)
finally:
    driver.quit()

 

auto_web_alert.py

import time
from selenium import webdriver
from selenium.webdriver.common.alert import Alert

driver = webdriver.Chrome('chromedriver')

try:
    driver.get('https://www.w3resource.com/javascript/alert-example1.html')

    time.sleep(1)

    alert = Alert(driver)
    alert.accept()

    input()

except Exception as e:
    print(e)
finally:
    driver.quit()

 

auto_web_instagram.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time

opts = webdriver.ChromeOptions()
opts.add_argument('user-data-dir=' + './Chrome')
driver = webdriver.Chrome('chromedriver', options=opts)

try:
    driver.get('https://www.instagram.com')
    input()

    time.sleep(2)
    elem = driver.find_element_by_class_name('eyXLr')
    action = ActionChains(driver)
    action.move_to_element(elem)
    action.click()
    action.send_keys('#파이썬')
    action.perform()

    time.sleep(2)

    action = ActionChains(driver)
    action.move_by_offset(0, 50)
    action.click()
    action.perform()

    time.sleep(5)

    elem = driver.find_element_by_class_name('EZdmt')
    posts = elem.find_elements_by_class_name('_9AhH0')
    for post in posts:
        action = ActionChains(driver)
        action.move_to_element(post)
        action.click()
        action.perform()

        time.sleep(1)

        elem = driver.find_element_by_class_name('fr66n')
        svg = elem.find_element_by_tag_name('svg')
        if svg.get_attribute('aria-label') == '좋아요':
            action = ActionChains(driver)
            action.move_to_element(elem)
            action.click()
            action.perform()
            time.sleep(1)

        action = ActionChains(driver)
        action.send_keys(Keys.ESCAPE)
        action.perform()

        time.sleep(1)
        input()

except Exception as e:
    print(e)
finally:
    driver.quit()

 

auto_web_jooonggonara.py

from selenium.webdriver.common.keys import Keys
from selenium import webdriver

driver = webdriver.Chrome('chromedriver')

try:
    driver.get('http://cafe.naver.com/joonggonara')

    elem = driver.find_element_by_id('topLayerQueryInput')
    elem.send_keys('자전거')
    elem.send_keys(Keys.RETURN)
    
    iframe = driver.find_element_by_id('cafe_main')
    driver.switch_to.frame(iframe)

    elem = driver.find_elements_by_class_name('article-board')[1]
    rows = elem.find_elements_by_xpath('./table/tbody/tr')
    for row in rows:
        elem = row.find_element_by_class_name('article')
        print(elem.text)

except Exception as e:
    print(e)
finally:
    driver.quit()

 

auto_web_wait.py

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome('chromedriver')

try:
    driver.get('https://www.instagram.com')

    wait = WebDriverWait(driver, 60)

    cond = EC.element_to_be_clickable((By.LINK_TEXT, '로그인'))
    btn = wait.until(cond)
    btn.click()

    input()

except Exception as e:
    print(e)
finally:
    driver.quit()

 

auto_web_window.py

from selenium import webdriver

driver = webdriver.Chrome('chromedriver')

try:
    driver.get('https://www.quirksmode.org/js/popup.html#create')

    btn = driver.find_element_by_link_text("Open popup")
    btn.click()

    wins = driver.window_handles
    driver.switch_to_window(wins[1])

    print(driver.title)

    input()

except Exception as e:
    print(e)
finally:
    driver.quit()

 

my_email.py

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL

SMTP_SERVER = 'smtp.naver.com'
SMTP_PORT = 465
SMTP_USER = 'lthlovelee'
# 실제 비밀번호를 입력해야 합니다.
SMTP_PASSWORD = ''


def send_mail(name, addr, contents, attachment=False):
    msg = MIMEMultipart('alternative')

    if attachment:
        msg = MIMEMultipart('mixed')

    msg['From'] = SMTP_USER
    msg['To'] = addr
    msg['Subject'] = name + '님, 메일이 도착했습니다.'

    text = MIMEText(contents)
    msg.attach(text)

    if attachment:
        from email.mime.base import MIMEBase
        from email import encoders

        file_data = MIMEBase('application', 'octet-stream')
        f = open(attachment, 'rb')
        file_contents = f.read()
        file_data.set_payload(file_contents)
        encoders.encode_base64(file_data)

        from os.path import basename
        filename = basename(attachment)
        file_data.add_header('Content-Disposition',
                'attachment', filename=filename)
        msg.attach(file_data)

    smtp = SMTP_SSL(SMTP_SERVER, SMTP_PORT)
    smtp.login(SMTP_USER, SMTP_PASSWORD)
    smtp.sendmail('lthlovelee@naver.com', addr, msg.as_string())
    smtp.close()
728x90
그리드형

댓글