강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

wogkeoqkr님의 프로필 이미지
wogkeoqkr

작성한 질문수

[신규 개정판] 이것이 진짜 크롤링이다 - 실전편 (인공지능 수익화)

텍스트를 포함하는 태그 찾기 (feat.정규표현식)

첫번째페이지크롤링 질문~!!!!

작성

·

615

·

수정됨

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=auto"

# 헤더에 User-Agent, Accept-Language 를 추가하지 않으면 멈춥니다
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") # select의 결과는 리스트 자료형
for link in links:
    sub_url = "https://www.coupang.com" + link.attrs['href']

    response = requests.get(sub_url, headers=header)
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')

    #try:
        #brand_name = soup.select_one("a.prod-brand-name").text
    #except:
        #brand_name = ""
    
    # 브랜드명
    #try:
        #brand_name = soup.select_one("a.prod-brand-name").text
    #except:
        #brand_name = ""
    #print(brand_name)
    
    # 상품명
    #product_name = soup.select_one("h2.prod-buy-header__title").text
    #product_name = ""

    # 가격
    #product_price = soup.select_one("span.total-price > strong").text
    #product_price = ""

    #print(brand_name, product_name, product_price)

 

for 문 안에 soup 를 print로 찍어보니

 <html lang="en"> <head> <script type="text/javascript"> document.location.replace("https://www.coupang.com/"); </script> </head> <body></body> </html>

<!DOCTYPE html>

<html lang="en"> <head> <script type="text/javascript"> document.location.replace("https://www.coupang.com/"); </script> </head> <body></body> </html>

 

이렇게 나와서 아래의 soup.select_one 값이 공백으로 나오네요.

brand_name = soup.select_one("a.prod-brand-name").text

 

강의랑 코드를 몇번씩 계속 돌려보는데도 이해가 안가네요

답변 1

0

스타트코딩님의 프로필 이미지
스타트코딩
지식공유자

image

안녕하세요!

제가 직접 수강생님의 코드를 복사해서

주석만 풀고 테스트해본 결과

브랜드명을 잘 가져오는 것으로 확인되었습니다.

 

코드 상에는 문제가 없는 것으로 판단되는데

재부팅 및 쿠키, 캐시 삭제 후 다시 실행해 보시겠어요?

 

wogkeoqkr님의 프로필 이미지
wogkeoqkr
질문자

제 PC 에서는 왜 안될까요..??

계속 데이터를 불러오질 못하는데

버젼도 최신버전확인 했는데

wogkeoqkr님의 프로필 이미지
wogkeoqkr

작성한 질문수

질문하기