18. Python - XML Parsing2
2021. 2. 24. 23:21
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 | from http.client import HTTPSConnection from urllib.parse import quote from xml.etree.ElementTree import fromstring # DEV.naver.com # X-Naver-Client-Id : ID # X-Naver-Client-Secret : PW # https://developers.naver.com/products/search/ # 요청 주소 : https://openapi.naver.com/v1/search/shop.xml # query # X-Naver-Client-Id : # X-Naver-Client-Secret : # 문자열 넣으면 글자 처리해주기 def cut(t): t = t.replace("<b>", "") t = t.replace("</b>", "") return t # 상품명 입력받기 q = input("상품명 : ") # 한글 처리 # URLEncoding # quote (* urllib.parse *) q = quote(q) # 요청 header 처리 # Python은 dict {}로 처리함 # 넘겨줘야하는 헤더를 dict로 처리 h = {"X-Naver-Client-Id": "ID", "X-Naver-Client-Secret": "PW"} hc = HTTPSConnection("openapi.naver.com") # hc.request("요청방식", "남은주소", headers=요청헤더) hc.request("GET", "/v1/search/shop.xml?query=" + q, headers=h) resbody = hc.getresponse().read() print(resbody.decode()) nShop = fromstring(resbody).getiterator("item") for n in nShop: print(cut(n.find("title").text)) print(cut(n.find("lprice").text)) print("------------") | cs |
'Python' 카테고리의 다른 글
20. Python - Web Crawling (0) | 2021.02.25 |
---|---|
19. Python - JSON Parsing (0) | 2021.02.25 |
17. Python - XML Parsing1 (0) | 2021.02.24 |
16. Python - Exception (0) | 2021.02.24 |
15. Python - 다중상속 (0) | 2021.02.23 |