인프런 커뮤니티 질문&답변
첫째 페이지 크롤링 오류
해결된 질문
작성
·
345
1
import requests
from bs4 import BeautifulSoup
main_url = "https://www.coupang.com/np/search?component=&q=%EA%B2%8C%EC%9D%B4%EB%B0%8D+%EB%A7%88%EC%9A%B0%EC%8A%A4&channel=user"
header = {
    'Host': 'www.coupang.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Language': 'ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3',}
response = requests.get(main_url, headers = header)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
links = soup.select("a.search-product-link")
for link in links:
    sub_url = "https://www.coupang.com/" + link.attrs['href']
    
    response = requests.get(sub_url,headers = header)
    html = response.text
    sub_soup = BeautifulSoup(html,'html.parser')
    #브랜드명 
    #중고상품 예외처리
    try: #아래 태그를 찾고 
        brand = soup.select_one("a.prod-brand-name")
    except:# 없으면 아래처럼 비우기 
        brand = "" 
    #상품명 
    name = soup.select_one("h2.prod-buy-header__title")
    
    #가격
    price = soup.select_one("span.total-price > strong")
    print(brand,name,price)
    
  
이렇게 작성 하고 실행 시키니 아무일도 일어나지 않았습니다. 그래서 ctrl + F5 로 실행 시키니 크롤링 된
데이터가 모두 none 으로 크롤링 되네요 .. 




