본문 바로가기

python

python oracle 디비에 넣기 (+oracle DB넣기)

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
import requests
import urllib
import cx_Oracle
import os
import chardet
from openpyxl import Workbook
from bs4 import BeautifulSoup
 
 
#####한글깨짐 방지######
os.environ["NLS_LANG"= ".AL32UTF8"
 
 
# Workbook을 통해 엑셀 파일을 생성
wb = Workbook()
 
# 현재 Active Sheet 얻기
ws = wb.active
ws.title = "first_Steet"
# 에겔 컬럼에 따른 컬럼명
ws["A1"= "brand"
ws["B1"= "name"
ws["C1"= "price"
ws["D1"= "src"
 
# DB와 연결된 코드
conn = cx_Oracle.connect('hr/1234@localhost:1521/orcl')
print(conn.version)
 
# 어떤 특정 웹 페이지를 크롤할 수 있게 하는 함수정의입니다. parameter는 최대 클롤할 페이지 수를 받습니다.
def spider(max_pages):
 
# page = 1 초기 입력해 주는 페이지가 최대 크롤할 페이지보다 작을 동안 계속 크롤할 수 있게 while 반복문 사용
    page = 1
    urlpage = 0
    i = 2
 
    while page <= max_pages:
 
# 크롤할 사이트를 본 블로그로 하고 그 뒤에 페이지 번호를 합쳐 저장한 값을 변수 url에 저장하도록 하였습니다.
        url1 = 'https://www.yslbeautykr.com/ko_KR/makeup/lips?sz=12&start='
        url = url1 + str(urlpage)
 
#  source_code = requests.get(url) 타겟 url로 부터 데이터를 저장할 수 있다.
        source_code = requests.get(url)
 
#  가져온 데이터를 source_code의 텍스트 부분만을 처리하여 plain_text에 저장
        plain_text = source_code.text
 
# 여기서 실행을 시키게 되면 무슨 내용이 있는지 잘 알수 가 없는 데이터로 가져오게 된다.
# 그래서, 아래와 같이 데이터를 사람이 일기 쉽게 해주는 파이썬 모듈이 초기에 불러온다.
# Beautifulsoup이라는 모듈이다.
# 가독성을 높이기 위해 Beautifulsoup(타겟, parser)를 처리하여 임의의 변수인 soup에 저장
        soup = BeautifulSoup(plain_text, 'html.parser')
 
        for div in soup.findAll('div', {'class''product_tile'}):
 
            p_tag = div.find('a', {'class''product_name desktop_content tablet_content'}).getText()
            name_tag = div.find('div', {'class''product_subtitle'}).getText()
            span_tag = div.find('p', {'class''product_price price_sale b-product_price-sale b-product_price'}).getText()
            img_tag = div.find('img')
 
            print(p_tag.strip())
            print(name_tag.strip())
            print(span_tag.strip())
            print(img_tag.get('data-desktop-src').strip())
 
            # nametemp = img_tag.get('alt')
            # if len(nametemp) == 0:
            #     filename = str(i)
            #     i = i + 1
            # else:
            #     filename = nametemp
            #
            # imagesfile = open(filename + ".jpeg", 'wb')
            # imagesfile.write(urllib.request.urlopen(img_tag.get('data-desktop-src')).read())
            # imagesfile.close()
 
            # ws.cell(row=i, column=1, value=p_tag.strip())
            # ws.cell(row=i, column=2, value=name_tag.strip())
            # ws.cell(row=i, column=3, value=span_tag.strip())
            # ws.cell(row=i, column=4, value=img_tag.get('data-desktop-src').strip())
 
 
 
            # insert
            sql_insert = 'insert into COSMATIC VALUES(:BRAND, :NAME, :PRICE, :SRC)'
            db = conn.cursor()
            # encode 디비에 들어가기는 하나... 이상한 값이 출력됨
            # encode()로 변환한다음 decode()로 한글 변환한다.
            db.execute(sql_insert, BRAND=p_tag.encode('utf8').decode('utf8'), NAME=name_tag.encode('utf8').decode('utf8'), PRICE=span_tag.encode('utf8').decode('utf8'), SRC=img_tag.get('data-desktop-src').encode('utf8').decode('utf8'))
 
            conn.commit()
 
            db.execute('SELECT * FROM COSMATIC')
 
            for record in db:
                print(record)
 
            i = i + 1
 
# while반복문이 무한반복이 되지 않게 하기 위해 page += 1을 주어 초기 페이지부터 지정한 특정페이지 번위의 모든 타이틀과 url값을 출력할 수 있다.
        page += 1
        urlpage += 12
 
 
    db.close()
    conn.close()
 
# 1페이지만 크롤링된다. 만약 spider(!0)이라면 1~9까지 스크롤하게 max_page값을 변경하게됩니다.
spider(2)
 
wb.save("Yves Saint Laurent.xlsx")
cs

'python' 카테고리의 다른 글

파이썬 서버 구축  (0) 2018.12.30
파이썬 오라클 연동 [python install oracle]  (0) 2018.12.28
파이썬 beautifulsoup함수 정리  (0) 2018.12.25
파이썬 웹크로링 및 엑셀정리  (0) 2018.12.24