python 특정단어 체크

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
python 특정단어 체크

QA

python 특정단어 체크

답변 1

본문

안녕하세요.

python에서 특정단어가 포함되어 있는지 체크할수 있는 방법이

있나요?

아래 div 안에 image 파일명에 특정단어를 확인해서

예를 들어

backImage_b8.png -> backImage 단어를 찾아서 파일을 분기하고 싶습니다.

div안에 3개의 파일이 있을수도 있고 2개의 파일이 있을수도 있어서

특정단어를 찾아서 분기를 해야 될거 같습니다.

감사합니다.

 

 

<div class="area_thumbnail">

<img src="https://aaa.com/backImage_b8.png" width="218" height="174">
<img src="https://aaa.com/frontImage_b8.png" width="218" height="174">
<img src="https://aaa.com/objectImage_b8.png" width="218" height="174">

</div>

 

python code

for a in soup.select('div[class="area_thumbnail"] img'):

                area_thumbnail  = a['src']

                if thumbnail_name == 'backImage': 

                    save1 = "backImage_1.png" 

                elif thumbnail_name == 'frontImage': 

                    save1 = "frontImage_1.png" 

                elif thumbnail_name == 'objectImage': 

                    save1 = objectImage_1.png"

이 질문에 댓글 쓰기 :

답변 1

문자열 메서드를 사용하면 가능하지 않을까 합니다.

예를 들어 다음과 같이 해 볼 수 있을 것 같습니다.


for a in soup.select('div[class="area_thumbnail"] img'):
    area_thumbnail = a['src']
    
    if 'backImage' in area_thumbnail:
        save1 = "backImage_1.png"
    elif 'frontImage' in area_thumbnail:
        save1 = "frontImage_1.png"
    elif 'objectImage' in area_thumbnail:
        save1 = "objectImage_1.png"
    else:
        # 처리해야 할 다른 경우가 있을 경우에 대한 로직 추가
        pass

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 103
© SIRSOFT
현재 페이지 제일 처음으로