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

파이썬 네이버 봇 만들기 - 내 일을 바꾸는 업무 자동화 9장

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

 

 

 

auto_web_naver.py

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

driver = webdriver.Chrome('./chromedriver')

try:
    driver.get('https://naver.com')
    keyword = input('검색어를 입력하세요:' )

    elem = driver.find_element_by_id('query')
    elem.send_keys(keyword)
    elem.send_keys(Keys.RETURN)

    div = driver.find_element_by_class_name('_blogBase')
    #elem = div.find_element_by_tag_name('ul')
    blogs = div.find_elements_by_xpath('./ul/li')

    for blog in blogs:
        title_tag = blog.find_element_by_class_name('sh_blog_title')
        print(title_tag.text)
        link = title_tag.get_attribute('href')
        print(link)
        pub_date_tag = blog.find_element_by_class_name('txt_inline')
        pub_date = pub_date_tag.text[:-4]
        print(pub_date)

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

 

auto_web_naver_result.py

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

driver = webdriver.Chrome('./chromedriver')

from openpyxl import Workbook
xlsx = Workbook()
sheet = xlsx.active
sheet.append(['Title', 'Link', 'Published date'])

try:
    driver.get('https://naver.com')
    keyword = input('검색어를 입력하세요:' )

    elem = driver.find_element_by_id('query')
    elem.send_keys(keyword)
    elem.send_keys(Keys.RETURN)

    div = driver.find_element_by_class_name('_blogBase')
    #elem = div.find_element_by_tag_name('ul')
    blogs = div.find_elements_by_xpath('./ul/li')


    for blog in blogs:
        title_tag = blog.find_element_by_class_name('sh_blog_title')
        link = title_tag.get_attribute('href')
        pub_date_tag = blog.find_element_by_class_name('txt_inline')

        sheet.append([title_tag.text, link, pub_date_tag.text])

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

file_name = 'result.xlsx'
xlsx.save(file_name)
from my_email import send_mail
send_mail('이태화', 'alghost.lee@gmail.com', '네이버 검색 결과입니다.', file_name)

 

auto_web_naver_tip.py

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

driver = webdriver.Chrome('./chromedriver')

try:
    driver.get('https://naver.com')
    keyword = input('검색어를 입력하세요:' )

    elem = driver.find_element_by_id('query')
    elem.send_keys(keyword)
    elem.send_keys(Keys.RETURN)

    div = driver.find_element_by_class_name('_blogBase')
    #elem = div.find_element_by_tag_name('ul')
    blogs = div.find_elements_by_xpath('./ul/li')
    
    for blog in blogs:
        title_tag = blog.find_element_by_class_name('sh_blog_title')
        title = title_tag.get_attribute('title')
        if not title:
            title = title_tag.text
        print(title)
        link = title_tag.get_attribute('href')
        #print(link)

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

 

auto_web_news.py

from selenium import webdriver

driver = webdriver.Chrome('./chromedriver')

driver.get('http://news.naver.com')
elem = driver.find_element_by_id('right.ranking_contents')
childs = elem.find_elements_by_tag_name('li')

for child in childs:
    print(child.text)

driver.quit()

 

auto_web_news_2.py

from selenium import webdriver

driver = webdriver.Chrome('./chromedriver')
try:
    driver.get('http://news.naver.com')
    elem = driver.find_element_by_id('right.ranking_contents')
    childs = elem.find_elements_by_tag_name('li')

    for child in childs:
        print(child.text)
except Exception as e:
    print(e)
finally:
    driver.quit()

 

exception_1.py

try:
    # 숫자로 이루어지지 않은 문자열은 무조건 에러 발생
    int('abcd')
    print("Try가 모두 실행되었습니다.")
except Exception as e:
    print("에러가 발생하였습니다.")
    print(e)
finally:
    print("Finally가 실행되었습니다.")

 

exception_2.py

try:
    # 숫자로 이루어진 문자열은 에러가 발생하지 않음
    int("100")
    print("Try가 모두 실행되었습니다.")
except Exception as e:
    print("에러가 발생하였습니다.")
    print(e)
finally:
    print("Finally가 실행되었습니다.")

 

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()

 

result.xlsx
0.01MB

728x90
그리드형

댓글