python_apps/crwlers/naver_get.py
2023-11-03 14:49:12 +09:00

91 lines
2.8 KiB
Python

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&viewType=list&sort=rel&frm=NVSHTTL&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.link')
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"^\<img\ alt\=\"|\"\ height\=.+$|\"\ src\=\".+$","",mall_name[0].decode_contents().strip())
btnCompareText = ''
else :
btnCompareText = btnCompare[0].getText().strip()
#print(btnCompareText)
if btnCompareText != '가격비교' :
itemType = '원부'
mallsText = ''
mallCounts = 0
malls = mallInfo[i].select('ul.mall_list')
mallsList = malls[0].select('li a._lowPriceByMall em span.mall_name')
if len(mallsList) == 0 :
mallsText = re.sub(r"^\<img\ alt\=\"|\"\ height\=.+$|\"\ src\=\".+$","",mall_name[0].decode_contents().strip())
else :
for j in range(len(mallsList)) :
mallName = mallsList[j].getText().strip()
mallsText = mallsText + ',' + mallName
mallCounts = mallCounts + 1
btnCompareText = re.sub(r"판매처","",btnCompareText)
else :
itemType = '가격비교상품'
mall_name = mallInfo[i].select('p.mall_txt > a:nth-of-type(1)')
mallsText = re.sub(r"^\<img\ alt\=\"|\"\ height\=.+$|\"\ src\=\".+$","",mall_name[0].decode_contents().strip())
btnCompareText = ''
print(
term + "\t" +
re.sub(r"전체","",res_count[0].getText().strip()) + "\t" +
str(num) + "\t" +
adOrNotText + "\t" +
itemType + "\t" +
productName + "\t" +
priceText + "\t" +
categoryText + "\t" +
re.sub(r"^\,","",mallsText) + "\t" +
btnCompareText + "\t" +
productURL
)
num = num + 1