파이썬 질문드리고 싶습니다
#노트북제품명찾기
import requests
from bs4 import BeautifulSoup
def laptop():
url = "https://search.shopping.naver.com/search/all.nhn?origQuery=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81&pagingIndex=1&pagingSize=40&productSet=model&viewType=list&sort=rel&frm=NVSHPRC&query=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81"
html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser")
tags = soup.find_all("li", {"class": "ad _model_list _itemSection"})
for idx, tag in enumerate(tags):
if tags.find("1050") == True:
print(idx, tags)
else:
print("원하시는 제품이 없습니다.")
laptop()
구현하고자 하는 것은 크롤링을 통해 모은 데이터 중에서 원하는 데이터가 있는 URL만 추출하는 것 인데요,
import requests from bs4 import BeautifulSoup
def laptopspec(): url = "https://search.shopping.naver.com/search/all.nhn?origQuery=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81&pagingIndex=1&pagingSize=40&productSet=model&viewType=list&sort=rel&frm=NVSHPRC&query=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81" html = requests.get(url).text
soup = BeautifulSoup(html, "html.parser") tags = soup.find_all("li", {"class": "ad _model_list _itemSection"})
for idx, tag in enumerate(tags): print(idx, tags)
laptopspec()
크롤링까지는 했는데 그 이후에 원하는 정보가 있는 부분의 URL을 뽑아낼 방법을 모르고 있습니다...
수고스러우시겠지만 답변해 주신다면 정말 감사드립니다ㅠㅠ
답변 2개
</p>
<p>import requests</p>
<p>from bs4 import BeautifulSoup</p>
<p> </p>
<p>def laptopspec():</p>
<p> url = "<a href="https://search.shopping.naver.com/search/all.nhn?origQuery=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81&pagingIndex=1&pagingSize=40&productSet=model&viewType=list&sort=rel&frm=NVSHPRC&query=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81"" target="_blank" rel="noopener noreferrer">https://search.shopping.naver.com/search/all.nhn?origQuery=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81&pagingIndex=1&pagingSize=40&productSet=model&viewType=list&sort=rel&frm=NVSHPRC&query=%EA%B2%8C%EC%9D%B4%EB%B0%8D%EB%85%B8%ED%8A%B8%EB%B6%81"</a></p>
<p> html = requests.get(url).text</p>
<p> </p>
<p> soup = BeautifulSoup(html, "html.parser")</p>
<p> tags = soup.find_all("li", {"class": "ad _model_list _itemSection"})</p>
<p> </p>
<p> for tag in tags:</p>
<p> print(tag.select_one('div.info a').text.strip())</p>
<p> print(tag.select_one('span.price em').text)</p>
<p> print(tag.select_one('div.img_area a')['href'])</p>
<p> </p>
<p>laptopspec()</p>
<p>
안녕하세요?
정확히 어떤 데이터를 크롤링하길 원하시는지 제가 알 수 없지만
이런 식으로 하면 제품명, 최저가, URL을 뽑을 수 있어요 ^^
그런데 ad _model_list _itemSection으로 class를 한정하여 find_all을 하면
현재 제가 확인하기로는 최상단 2개 제품(광고) 밖에 크롤링이 되지 않네요.
의도하신 부분인지는 모르겠네요~
그럼 감기 조심하시고 좋은 주말 되세요~!

댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인