테스트 사이트 - 개발 중인 베타 버전입니다

python 질문드립니다. srcset 이미지 가져오기 채택완료

망고프리 1년 전 조회 4,204

안녕하세요.

python 으로 아래 문장 크롤링을 하고 싶습니다.

아래 같은 똑같은 문장이 100개정도 있으므로 for문으로 계속가져와야 합니다.

div.w-full.absolute.left-0.bottom-10.flex-center.flex-col

안에있는 srcset webp, png, 제목을 가져오고 싶습니다.

감사합니다.

 

 

epi_url         = urlopen('https://aaa.com/1.php')

soup            = BeautifulSoup(epi_url.read(), 'html.parser')

imgs            = soup.select('div.w-full.absolute.left-0.bottom-10.flex-center.flex-col img') #요소 선택

i = 1

for anchor in imgs.select("source"):      

     main_img    = anchor.get('srcset')

     anchor_type = anchor.get('type')

     

      webp = "webp 이미지 가져오기"

      png   = "png 이미지 가져오기"

      title   = "오징어 말이 된다  가져오기"

 

 

=== 1.php =====

<div class="w-full absolute left-0 bottom-10 flex-center flex-col">
                    <picture class="">
                        <source type="image/webp" srcset="https://aaa.com/688eca70-7166-4b54-a9e1-3309d6e0b412.webp">
                        <source type="image/png" srcset="https://aaa.com/688eca70-7166-4b54-a9e1-3309d6e0b412.png">
                        <img src="aaa.com/688eca70-7166-4b54-a9e1-3309d6e0b412.png" alt="오징어 말이 된다" class="w-full max-w-[115px] lg:max-w-[150px] mx-auto my-0">
                    </picture>
                    <p class="whitespace-pre-wrap break-all break-words support-break-word overflow-hidden text-ellipsis opacity-70 s10-regular-white px-8 mt-4 text-center" style="width: 100%; -webkit-line-clamp: 1; -webkit-box-orient: vertical; display: -webkit-box;">
                        강아지, 고양이
                    </p>
                </div>

 

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
1년 전

wfull = soup.select('div.w-full')

for picture in (wfull):
  main_img = picture.find("img")["src"]
  title = picture.find("img")["alt"]

  source = picture.select("source")    
  webp = source[0]['srcset'] 
  png   = source[1].get('srcset')

 

webp, png 두가지 위와 같이해도 동일한 처리를 합니다

 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

망고프리
1년 전
잘 작동하네요
감사합니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인