첫번째페이지크롤링 질문~!!!!
627
작성한 질문수 2
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
51
2
네이버 로그인 관련
0
274
2
안녕하세요 셀레니움에 대해서 질문
0
87
1
크롤링 연습사이트 문의
0
97
2
선택자 질문
0
72
2
'특정 요소가 나타날 때까지 스크롤' 부분 에러
0
78
2
자동 로그인 질문
0
89
2
44강 제목, 링크
0
107
1
원하는 값이 없을 때
0
91
2
크롤링한 링크가 엑셀로 들어가면 작동이 안되요
0
228
2
셀레니움 PDF자료는 받을 수 있나요
0
100
2
글목록 추출하기
0
100
2
메일 자동화 로그인 중복방지문자해결 오류 및 명시적 대기 질문
0
91
2
강의 노트가 어디에 있는건가요?
0
80
2
강의 커리큘럼 질문
0
101
1
조건문 else 사용하지 않는 이유
0
77
2
셀레니움으로 접근할 수 없는 경우
0
96
2
웹페이지 변경
0
73
2
자바스크립트로 태그 선택 시 질문입니다.
1
65
2
수료증은 어떻게 받나요?
0
120
2
class명을 활용하여 선택자를 만들지 않는 경우..?
0
61
2
드라이버가 안 열려요
0
79
2
이거 해결방법 아시는 분?
0
121
2
네이버 지식인 크롤링..
0
202
2






