import requests, bs4, urllib, sys, re
term = str(sys.argv[1]);
query = urllib.parse.quote_plus(term)
targetUrl = "https://search.shopping.naver.com/search/all.nhn?origQuery=" + query + "&pagingIndex=1&pagingSize=80&productSet=model&viewType=list&sort=rel&frm=NVSHMDL&query=" + query
resp = requests.get(targetUrl)
resp.raise_for_status()
resp.encoding='UTF-8'
html = resp.text
bs = bs4.BeautifulSoup(html, 'html.parser')
res_count = bs.select('ul.snb_list li.snb_all')
_lists = bs.select('li._itemSection div.info')
mallInfo = bs.select('li._itemSection div.info_mall')
if len(res_count) == 0 :
print(term + "\t" + 'Not Found')
else :
num = 1
for i in range(len(_lists)) :
name = _lists[i].select('a.tit')
productName = name[0].getText().strip()
productURL = name[0].get('href')
priceInt = _lists[i].select('span.price em span.num')
priceText = priceInt[0].getText().strip()
adOrNot = _lists[i].select('span.price a.ad_stk')
if len(adOrNot) != 0 :
adOrNotText = adOrNot[0].getText().strip()
else :
adOrNotText = ''
catedepth = _lists[i].select('span.depth')
if len(catedepth) == 0 :
categoryText = ''
else :
categoryText = re.sub(r"\t|\n|\s\s","",catedepth[0].getText())
#원부/상품구분
btnCompare = _lists[i].select('span.price a.btn_compare')
if len(btnCompare) == 0 :
itemType = '상품'
mall_name = mallInfo[i].select('p.mall_txt > a:nth-of-type(1)')
mallsText = re.sub(r"^\
a:nth-of-type(1)')
mallsText = re.sub(r"^\![]()