• 카테고리

    질문 & 답변
  • 세부 분야

    데이터 분석

  • 해결 여부

    미해결

2-8-2문제 질문 드립니다.

20.09.12 19:53 작성 조회수 117

0

인프런 홈페이지 내용이 달라져서 혼자 https://www.inflearn.com/roadmaps 부분으로 공부해보려고 했는데요. 일단 다른 분들 질문한 것 까지 다 읽어서 뭐가 잘못됐는지 확인해보려고 했는데 잘 모르겠습니다 ㅠㅠ 

이미지랑 text 다운로드는 되는데 딱 하나밖에 안되네요.. 도움주시면 정말 감사하겠습니다.

from bs4 import BeautifulSoup

import urllib.request as req

import urllib.parse as rep

import sys

import io

import os

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')

sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')

base = "https://www.inflearn.com/"

quote = rep.quote_plus("roadmaps")

url = base+quote

res = req.urlopen(url)

savePath = "C:\\imagedown\\"

try:

    if not (os.path.isdir(savePath)):

        os.makedirs(os.path.join(savePath))

except OSError as e:

    if e.errno != errno.EEXIST:

        print("폴더 만들기 실패")

        raise

soup = BeautifulSoup(res, "html.parser")

recommand = soup.select("div.view")

print(recommand)

for i,e in enumerate(recommand, 1):

    with open(savePath+"title_"+str(i)+".txt","wt") as f:

        f.write(e.select_one(".content > h5").string)

    fullFileName = os.path.join(savePath, savePath+str(i)+'.png')

    req.urlretrieve(e.select_one("figure.image > img")['src'],fullFileName)

print('다운로드 완료')

답변 1

답변을 작성해보세요.

1

안녕하세요.

선택자, for문 구문을 살펴보세요.

다중 리스트를 가져온 후 개수 만큼 for문으로 출력하기 때문에

애초에 인프런 요청 후 선택자 부분이 잘못되었을 가능성이 있습니다.