# data[].attributes.body
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

current_path = os.path.dirname(__file__)
driver_path = os.path.join(current_path, "chromedriver")
driver = os.path.join(driver_path, "chromedriver.exe")
browser = webdriver.Chrome(driver) #드라이버
browser.get("https://flight.naver.com/")

#출발지 선택
browser.find_element(By.XPATH,"//*[@id='__next']/div/div[1]/div[4]/div/div/div[2]/div[1]/button[1]").click()
#국내선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/section/section/button[1]").click()
#김포선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/section/section/button[4]").click()
#도착지 선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]div[2]/section/section/button[1]").click()
#국내 선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]div[2]/section/section/div/button[2]").click()
#제주도 선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/div[1]/div[2]/div/div[2]/table/tbody/tr[3],td[6]/button").click()
#가는 날 선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[5],td[6]/button").click()
#가는날짜 선택
browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[4]/div/div/button").click()
#오는날짜 선택
time.sleep(20)
# 성인 2명, 할인/일반/특가석 선택
#항공권 선택

# data[].attributes.body
browser.find_element(By.XPATH,"//*[@id='__next']/div/div[1]/div[4]/div/div/div[2]/div[1]/button[1]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/section/section/button[1]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/section/section/button[4]").click()

browser.find_element(By.XPATH,"//*[@id='__next']/div/div[1]/div[4]/div/div/div[2]/div[1]/button[2]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]div[2]/section/section/button[1]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]div[2]/section/section/div/button[2]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[4]/div/div/div[2]/div[2]/button[1]").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/div[1]/div[2]/div/div[2]/table/tbody/tr[3],td[6]/button").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[10]/div[2]/div[1]/div[2]/div/div[3]/table/tbody/tr[5],td[6]/button").click()

browser.find_element_by_xpath("//*[@id='__next']/div/div[1]/div[4]/div/div/button").click()

time.sleep(20)

# data[].attributes.body
from bs4 import BeautifulSoup
import re
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.118 Whale/2.11.126.22 Safari/537.36"}
for i in range(16, 21):
    url = "https://search.naver.com/search.naver?where=nexearch&sm=tab_etc&qvt=0&query=20" + str(i) + "%EB%85%84%20%EC%98%81%ED%99%94%20%EC%88%9C%EC%9C%84"
    res = requests.get(url, headers=headers)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, "lxml")
    images = soup.find_all("img")
    nms = soup.find_all("strong", attrs={"class":"scm_ellipsis_text"})
    for cnt, img in enumerate(images): # ChOihT
        if cnt >= 5:
            break
        i_src = img["src"]
        i_res = requests.get(i_src)
        i_res.raise_for_status()
        with open("movie" + str(i) + "_" + str(cnt + 1) + "_" + nms[cnt].get_text().replace(":", "-") + ".jpg", "wb") as f:
            f.write(i_res.content)

# data[].attributes.body
################################## selenium(다음 로그인) #####################

import os

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

import time

current_path = os.path.dirname(__file__)

driver_path = os.path.join(current_path, "chromedriver")

driver = os.path.join(driver_path, "chromedriver.exe")

# print(driver)

browser = webdriver.Chrome(driver)

browser.get("https://www.daum.net/")

elem = browser.find_element(By.XPATH, "//*[@id='inner_login']/a[2]")

elem.click()

# browser.find_element(By.XPATH, "//*[@id='inner_login']/a[2]").click()

id = browser.find_element(By.ID, "id")

id.send_keys("daum-id")

pw = browser.find_element(By.ID, "inputPwd")

pw.send_keys("daum-pwd")

loginBtn = browser.find_element(By.ID, "loginBtn")

loginBtn.click()

time.sleep(5)

# data[].attributes.body
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
current_path = os.path.dirname(__file__)
driver_path = os.path.join(current_path, "chromedriver")
driver = os.path.join(driver_path, "chromedriver.exe")
browser = webdriver.Chrome(driver)
browser.get("http://www.daum.net")
elem = browser.find_element(By.CLASS_NAME, "link_login")
elem.click()
id = browser.find_element(By.NAME, "email")
id.send_keys("id")
pw = browser.find_element(By.NAME, "password")
pw.send_keys("password")
pw.send_keys(Keys.ENTER)
time.sleep(10)

# data[].attributes.body
# import os

# from selenium import webdriver

# from selenium.webdriver.common.keys import Keys

# from selenium.webdriver.common.by import By

# import time

# current_path = os.path.dirname(__file__)

# driver_path = os.path.join(current_path, "chromedriver")

# driver = os.path.join(driver_path, "chromedriver.exe")

# # print(driver)

# browser = webdriver.Chrome(driver)

# # 1. url("https://www.naver.com")접속

# browser.get("https://www.naver.com")

# # 2. 로그인 앨리먼트 검색

# # elem = browser.find_element_by_class_name("link_login")

# elem = browser.find_element(By.CLASS_NAME, "link_login")

# # 3. 로그인 버튼 클릭

# elem.click()

# # 4. id 앨리먼트검색

# # id = browser.find_element_by_id("id")

# id = browser.find_element(By.ID, "id")

# # 5. 아이디 입력

# id.send_keys("naver-id")

# # 6. 패스워드 앨리먼트 검색

# # pw = browser.find_element_by_id("pw")

# pw = browser.find_element(By.ID, "pw")

# # 7. 패스워드 입력

# pw.send_keys("naver-pw")

# # 8. 로그인 실행(엔터키 입력)

# # pw.send_keys(Keys.ENTER)

# # 로그인 버튼 클릭

# # btn_login = browser.find_element_by_id("log.login")

# browser.find_element(By.ID, "log.login").click()

# # btn_login.click()

# time.sleep(3)

# # 아이디, 비번 삭제 후 재입력

# # id = browser.find_element_by_id("id")

# id = browser.find_element(By.ID, "id")

# # pw = browser.find_element_by_id("pw")

# pw = browser.find_element(By.ID, "pw")

# id.clear()

# pw.clear()

# time.sleep(3)

# id.send_keys("new_naver_id")

# pw.send_keys("new_naver_pwd")

# # btn_login = browser.find_element_by_id("log.login")

# # btn_login.click()

# browser.find_element(By.ID, "log.login").click()

# # print(elem)

# time.sleep(5)

# data[].attributes.body
####################################웹에서 데이터 CSV 저장 ########################

from bs4 import BeautifulSoup

import csv

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"}

url = "https://finance.naver.com/sise/sise_market_sum.nhn"

res = requests.get(url,headers=headers)

res.raise_for_status()

soup = BeautifulSoup(res.text, "lxml")

filename = "시가총액1-50.csv"

fd = open(filename, "w", encoding="utf-8-sig", newline="")

writer = csv.writer(fd)

data_head = soup.find("table", attrs={"class":"type_2"}).find("thead").find_all("th")

tmp_str = list()

for dh in data_head:

tmp_str.append(dh.get_text())

writer.writerow(tmp_str) #실제 csv형태로 파일 저장

data_row = soup.find("table", attrs={"class":"type_2"}).find("tbody").find_all("tr")

for row in data_row:

columns = row.find_all("td")

if len(columns) <=1:

continue

data = [column.get_text().strip() for column in columns]

writer.writerow(data)

# data[].attributes.body
#     print("-" * 100)

# ---------------------

from bs4 import BeautifulSoup

import re

headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.118 Whale/2.11.126.22 Safari/537.36"}

url = "https://movie.naver.com/movie/bi/mi/basic.naver?code=184311"

res = requests.get(url, headers=headers)

res.raise_for_status()

soup = BeautifulSoup(res.text, "lxml")

# images = soup.find_all("img")

# i_src = images[-10]["src"]

# i_res = requests.get(i_src)

# i_res.raise_for_status()

# print(i_src)

#

# with open("movie.jpg", "wb") as f:

#     f.write(i_res.content)

images = soup.find_all("img")

for cnt, img in enumerate(images):

i_src = img["src"]

i_res = requests.get(i_src)

i_res.raise_for_status()

with open("movie" + str(cnt) + ".jpg" , "wb") as f:

f.write(i_res.content)

if cnt > 10:

break

# data[].attributes.body
name[idx].get_text().replace(":","-")

# data[].attributes.body
from bs4 import BeautifulSoup
import re
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.118 Whale/2.11.126.22 Safari/537.36"}
url = "https://movie.naver.com/movie/bi/mi/basic.naver?code=184311"
res = requests.get(url, headers=headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")
# images = soup.find_all("img")
# i_src = images[-10]["src"]
# i_res = requests.get(i_src)
# i_res.raise_for_status()
# print(i_src)
#
# with open("movie.jpg", "wb") as f:
#     f.write(i_res.content)
images = soup.find_all("img")
for cnt, img in enumerate(images):
    i_src = img["src"]
    i_res = requests.get(i_src)
    i_res.raise_for_status()
    with open("movie" + str(cnt) + ".jpg" , "wb") as f:
        f.write(i_res.content)
    if cnt > 10:
        break

# data[].attributes.body
from bs4 import BeautifulSoup

import re

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}

for i in range(1, 5):

url = "https://www.coupang.com/np/search?q=노트북&channel=user&component=&eventCategory=SRP&trcid=&traid=&sorter=scoreDesc&minPrice=&maxPrice=&priceRange=&filterType=&listSize=36&filter=&isPriceRange=false&brand=&offerCondition=&rating=0&page={}&rocketAll=false&searchIndexingToken=1=5&backgroundColor=".format(i)

res = requests.get(url, headers=headers)

res.raise_for_status()

soup = BeautifulSoup(res.text, "lxml")

items = soup.find_all("li", attrs={"class":re.compile("^search-product")})

for item in items:

ad_badge = item.find("span", attrs={"class":"ad-badge-text"})

# if(item.find(class_="rating")):

# if(item.find(attrs={"class":"rating"})):

# if(item.find("em", attrs={"class":"rating"})):

#         rating = rating = item.find("em", attrs={"class":"rating"}).get_text()

if(item.find(class_= "rating")):

rating = item.find(attrs={"class":"rating"}).get_text()

else:

rating = 0

# if(item.find("span", attrs={"class":"rating-total-count"})):

#         rating_count = item.find("span", attrs={"class":"rating-total-count"}).get_text()

# else:

#         rating_count = 0

if(item.find(class_= "rating-total-count")):

rating_count = item.find(class_= "rating-total-count").get_text()

else:

rating_count = 0

name = item.find("div", attrs={"class":"name"}).get_text()

price = item.find("strong", attrs={"class":"price-value"}).get_text()

link = item.find("a", attrs={"class":"search-product-link"})["href"]

rocket = item.find("span", attrs={"class":"rocket"})

if ad_badge:

continue

# print("광고제품")

if float(rating) != 0:

continue

# print("별점이 4.5이하인 제품")

if int(rating_count) != 0 and (rating_count[1:-1]) < 3000:

# continue

# print("리뷰수가 3000이상인 제품")

print("")

#      if rocket:

#             continue

#             print("로켓배송 제품")

print("제품명 : {}".format(name))

print("별점 : {}".format(rating))

print("리뷰수 : {}".format(rating_count))

print("가격 : {}".format(price))

print("제품 링크 : {}".format("https://www.coupang.com"+link))

# data[].attributes.body
from bs4 import BeautifulSoup

from bs4 import BeautifulSoup

import re

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"}

for i in range(1, 5):

print("현재 페이지 : {}".format(i))

url = "https://www.coupang.com/np/search?q=%EB%85%B8%ED%8A%B8%EB%B6%81&channel=user&component=&eventCategory=SRP&trcid=&traid=&sorter=scoreDesc&minPrice=&maxPrice=&priceRange=&filterType=&listSize=36&filter=&isPriceRange=false&brand=&offerCondition=&rating=0&page={}&rocketAll=false&searchIndexingToken=1=5&backgroundColor=".format(i)

res = requests.get(url, headers=headers)

res.raise_for_status()

soup = BeautifulSoup(res.text, "lxml")

items = soup.find_all("li", attrs={"class":re.compile("^search-product")})  #해당 문자열로 시작하는 모든 문자열

for item in items:

ad_badge = item.find("span", attrs={"class":"ad-badge-text"})

# if(item.find(class_="rating")):                                       # 별점이 없는 제품

# if(item.find(attrs={"class":"rating"})):                              # 별점이 없는 제품

# if(item.find("em", attrs={"class":"rating"})):                        # 별점이 없는 제품

#         rating = rating = item.find("em", attrs={"class":"rating"}).get_text()

if(item.find(class_= "rating")):                                        # 별점이 없는 제품

rating = item.find(attrs={"class":"rating"}).get_text()

else:

rating = 0

# if(item.find("span", attrs={"class":"rating-total-count"})):            #리뷰수가 없는 제품

#         rating_count = item.find("span", attrs={"class":"rating-total-count"}).get_text()

# else:

#         rating_count = 0

if(item.find(class_= "rating-total-count")):            #리뷰수가 없는 제품

rating_count = item.find(class_= "rating-total-count").get_text()

else:

rating_count = 0

name = item.find("div", attrs={"class":"name"}).get_text()              #제품 이름(스팩)

price = item.find("strong", attrs={"class":"price-value"}).get_text()   #제품 가격

link = item.find("a", attrs={"class":"search-product-link"})["href"]    #제품 링크

rocket = item.find("span", attrs={"class":"rocket"})                    #로켓배송 제품

if ad_badge:

continue

# print("광고제품입니다.")

if float(rating) != 0:

continue

# print("별점이 4.5이하인 제품")

if int(rating_count) != 0 and (rating_count[1:-1]) < 3000:

# continue

# print("리뷰수가 3000이상인 제품")

print("")

#      if rocket:

#             continue

#             print("로켓배송 제품입니다.")

print("제품명 : {}".format(name))

print("별점 : {}".format(rating))

print("리뷰수 : {}".format(rating_count))

print("가격 : {}".format(price))

print("제품 링크 : {}".format("https://www.coupang.com"+link))

print("-" * 100)

#      print("-" * 100)

# data[].attributes.body
from bs4 import BeautifulSoup
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.118 Whale/2.11.126.22 Safari/537.36"}
for i in range(1, 5):
    print(i)
    url = "https://www.coupang.com/np/search?rocketAll=false&q=rtx3090&brand=&offerCondition=NEW&filter=1%23attr_12344%2424591attr_12337%2418998%40DEFAULT&availableDeliveryFilter=&filterType=&isPriceRange=false&priceRange=&minPrice=&maxPrice=&page={}&trcid=&traid=&filterSetByUser=true&channel=user&backgroundColor=&component=178155&rating=0&sorter=scoreDesc&listSize=36".format(i)
    res = requests.get(url, headers=headers)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, "lxml")
    items = soup.find_all("li", attrs={"class":re.compile("^search-product")})
    for item in items:
        rc = r = "0"
        rt = "NotRocketDelivery"
        ab = item.find("span", attrs={"class":"ad-badge-text"})
        if item.find("em", attrs={"class":"rating"}) != None:
            r = item.find("em", attrs={"class":"rating"}).get_text()
            rc = item.find("span", attrs={"class": "rating-total-count"}).get_text()
        n = item.find("div", attrs={"class":"name"}).get_text()
        p = item.find("strong", attrs={"class":"price-value"}).get_text()
        if item.find("span", attrs={"class":"rocket"}) != None:
            rt = "RocketDelivery"

        l = "https://www.coupang.com" + item.find("a", attrs={"class":"search-product-link"})["href"]
        if ab:
            continue;
        if float(r) < 4.5:
            continue
        if int(rc[1:-1]) < 1:
            continue

        print(l, rt)

# data[].attributes.body
from bs4 import BeautifulSoup
import re

headers = {"User Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}

url = "https://search.daum.net/search?nil_suggest=btn&w=tot&DA=SBC&q=2020%EB%85%84+%EC%98%81%ED%99%94%EC%88%9C%EC%9C%84"
res = requests.get(url, headers=headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")
images = soup.find_all("img",attrs={"class":"thumb_img"})

#print(type(images))

for idx, image in enumerate(images):
image_src = image["src"]
image_res = requests.get(image_src)

image_res.raise_for_status()

whih open("movie.jpg" + str(idx+1) + ".jpg" , "wb") as f:
f.writr(image_res.content) #movie.jpg, movie2.jpg 차례대로  들어가야함

if idx > 4:
break

# data[].attributes.body
30511 이재원