part1 : 매인 페이지 가져오기
part2 :상세 페이지 가져오기
part3 : 리뷰 가져오기
해당 프로젝트를 진행하면서 크롤링 했던자료는 모두 개인프로젝트 끝난 후 삭제 했음을 알립니다.
추가적으로 해야하는 가져와야 하는 부분은 iframe입니다.

frame이란 inline frame의 약자입니다.
iframe 요소를 이용하면 해당 웹 페이지 안에 어떠한 제한 없이 또 다른 하나의 웹 페이지를 삽입할 수 있습니다.
part2에서 상세페이지 정보를 가져오는 정보와 + iframe을 가져 오시면 되겠습니다.
아래의 코드는 리뷰를 5페이지만 (30개) 가져오는 로직입니다.
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import random
import csv
import requests
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument("headless")
chrome_option.add_argument("-disable-gpu")
chrome_option.add_argument("lang-=ko_KR")
driver = webdriver.Chrome(
"/home/spectre/바탕화면/Python/crawling/python/chromedriver",
chrome_options=chrome_option,
)
data_list = []
def get_detail_real_data(info_box, id):
for info in info_box:
like_name = info.find(
"div", {"class": "products_reviews_list_review__score_text_rating"}
).text
if info.find_all("div", {"class": "review_option"}):
try:
worried = info.find_all("div", {"class": "review_option"})[0].find("div", {"class": "review_option__content"}).text
oilyskin = info.find_all("div", {"class": "review_option"})[1].find("div", {"class": "review_option__content"}).text
except Exception:
worried = (info.find_all("div", {"class": "review_option"})[0].find("div", {"class": "review_option__content"}).text)
oilyskin=""
else:
worried = ""
oilyskin = ""
content = info.find("div", {"class": "review_message"}).text # 메시지
content = content.strip()
try:
image_url = "http:" + info.find("img")["src"]
except Exception:
image_url =""
writer = 1
data_list.append(
{
"id": id,
"like_name": like_name,
"worried": worried,
"oilyskin": oilyskin,
"content": content,
"image_url": image_url,
}
)
def get_detail_data(url, id):
html_code = requests.get(url).text
bs = BeautifulSoup(html_code, "html.parser")
info_boxs = bs.find("div", {"class": "page"})
info_box = info_boxs.find_all("li", {"class": "product_review__container"})
get_detail_real_data(info_box, id)
CSV_PATH_PRODUCTS = "/home/spectre/바탕화면/Python/crawling/python/products_review_url.csv"
# 파일읽기
with open(CSV_PATH_PRODUCTS) as in_file:
data_reader = csv.reader(in_file)
next(data_reader, None)
for row in data_reader:
for i in range(1, 5):
get_detail_data(row[1] + f"&page={i}", row[0])
print(row[1], f"&page={i}")
# 해당 파일을 csv파일로 만들기
def csv_save(star_menu):
f = open("products_detail_review.csv", "w", encoding="utf-8", newline="")
wr = csv.writer(f)
wr.writerow(
["id", "like_name", "worried", "oilyskin", "content", "image_url",]
)
for Info in star_menu:
wr.writerow(
[
Info["id"],
Info["like_name"],
Info["worried"],
Info["oilyskin"],
Info["content"],
Info["image_url"],
]
)
f.close()
csv_save(data_list)
가져온 자료

이것으로 닥터자르트 홈페이지의 기본적인 자료를 다 가져왔습니다.
다음 포스팅으로는 자동으로 DB에 넣는 과정을 하겠습니다.
'Djnago > 1차 프로젝트' 카테고리의 다른 글
닥터자르트 Dr.Jart+ 크롤링 Part 2 (0) | 2020.08.14 |
---|---|
닥터자르트 Dr.Jart+ 크롤링 Part 1 (0) | 2020.08.13 |
wecode 1차 프로젝트 후기 (0) | 2020.08.05 |
[1차] 스크럼 방법 (0) | 2020.07.22 |
댓글