파이썬 (pythoon)

[python] selenium / 쇼핑몰 제품 크롤링 / 엑셀 저장 / openpyxl workbook/ append / find_element_by_class

working for you 2021. 7. 9. 20:31
반응형

[셀레늄을 이용하여 크롤링]

find_element_by_class_name

find_element_by_tag_name

find_element_by_id

find_element_by_xpath

 

.get_attribute('href') # 태그속성

.text # 텍스트속성

 

일반 list : 리스트변수.append('내용1', '내용2', '내용3', ',,,')

 

[엑셀양식 저장하기]

xlsx save list  : ts.append(['내용1', '내용2', '내용3', ',,,'])

for data in ts: 

    ws.append(data)

wb.save('./ex.xlsx')

 

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
from selenium import webdriver
from openpyxl import Workbook
 
driver = webdriver.Chrome('./chromedriver.exe')
 
try:
    driver.get('http://mall.kidkids.net/')
    driver.find_element_by_class_name('p_close').click()
 
    element = driver.find_element_by_class_name('hot_banner_area')
    li = element.find_elements_by_tag_name('li')
 
    su_list = []
    su = []
    ts = []
    for i in li:
 
        # print(i.text)
        a = i.find_element_by_xpath('./a').get_attribute('href')
        name = i.find_element_by_xpath("./a/div[@class='sch_txt']/h4")
        price = i.find_element_by_class_name("num")
        print(name.text)
        print('링크주소 : ' + str(a))
        print(price.text)
 
        su.append(name.text)
        su.append(price.text)
        su.append(a)
 
        ts.append([name.text,price.text,a]) # 엑셀 저장양식 1줄씩
 
    su_list.append(su) #리스트 형식으로 저장하기
    
    wb = Workbook()
    ws = wb.active
 
    for data in ts: # 엑셀 저장 1줄씩 생성
        ws.append(data)
 
    wb.save('./ex.xlsx')
 
finally:
    driver.quit()
cs

[결과값]

크롤링 후 엑셀 저장 완료

반응형