인프런 커뮤니티 질문&답변
product_price = soup.selet_one("span.total-price>strong").text에서 'NoneType' object is not callable
작성
·
376
답변 2
1
0
스타트코딩
지식공유자
안녕하세요~!
데이터가 나와야 하는데, 안나오면 정말 당황 스럽죠 ㅎㅎ.
해당 코드를 전체를 확인할 수 없어서 이유를 파악하기 어렵네요.
제가 usb허브로 검색했을때는 잘 동작합니다.
아래 코드와 비교해서 확인해 보세요 :)
import requests
from bs4 import BeautifulSoup
main_url = "https://www.coupang.com/np/search?component=&q=usb%ED%97%88%EB%B8%8C&channel=user"
# 헤더에 User-Agent를 추가하지 않으면 오류가 나요 (멈춰버림)
response = requests.get(main_url, headers={'User-Agent' : 'Mozila/5.0'})
html = response.text
soup = BeautifulSoup(html, 'html.parser')
links = soup.select("a.search-product-link") # select의 결과는 리스트 자료형
for link in links:
# 광고 상품 제거
if len(link.select("span.ad-badge-text")) > 0:
print("광고 상품 입니다.")
else:
sub_url = "https://www.coupang.com/" + link.attrs['href']
response = requests.get(sub_url, headers={'User-Agent' : 'Mozila/5.0'})
html = response.text
soup = BeautifulSoup(html, 'html.parser')
# 브랜드명은 있을 수도 있고, 없을 수도 있어요
# - 중고상품일 때는 태그가 달라져요
# try - except로 예외처리를 해줍니다
try:
brand_name = soup.select_one("a.prod-brand-name").text
except:
brand_name = ""
# 상품명
product_name = soup.select_one("h2.prod-buy-header__title").text
# 가격
product_price = soup.select_one("span.total-price > strong").text
print(brand_name, product_name, product_price)






아 저건 저도 못찾았네요 ㅎㅎ