python2019. 12. 26. 17:07

 

Shopping에서 iphone 검색 순위 1000개 가져오기

 

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
import requests
import pprint
import openpyxl
 
client_id ='XiNJxT192mk123'
client_secret = 'dpolse'
 
excel_file = openpyxl.Workbook()
excel_sheet = excel_file.active
excel_sheet.column_dimensions['B'].width = 100
excel_sheet.column_dimensions['C'].width = 100
excel_sheet.append(['rank','title','link'])
 
 
#100개의 결과 가져오기 , display는 최대 100으로 지정가능하므로 start 위치를 변경시켜 반복 한다
start_num, item_index = 1, 1
for index in range(10):
    start_num =  1+index*100
     
     
    naver_open_api = naver_open_api + str(start_num)
    #print(naver_open_api)
    
    header_params = {'X-Naver-Client-Id':client_id ,'X-Naver-Client-Secret':client_secret}
 
    res = requests.get(naver_open_api, headers=header_params)
 
    if res.status_code == 200:
        data = res.json()
        for item in data['items']:
            #print(item_index, item['title'],item['link'])
            excel_sheet.append([item_index, item['title'], item['link']])
            item_index += 1
 
    else:
        print("Error code : ",res.status_code)
         
         
excel_file.save('shpping_1000.xlsx')
excel_file.close()
Posted by easy16