python select_one 질문드립니다.
본문
안녕하세요.
1. python soup.select_one('') 으로 아래 적색부분 url을 가져올려고 하는데
https://website.com/2021/0910/202109101125338484700.jpg
어떻게 가져와야 되는지
<div class="container mt-2">
<div class="row list-item">
<div class="col-12 col-sm-7 col-md-7 mt-2">
<div class="card card-fluid">
<script>
document.write('<img src="'+img_domain+'2021/0910/202109101125338484700.jpg'+'" style="width:100%">');
</script>
<img src="https://website.com/2021/0910/202109101125338484700.jpg" style="width:100%">
<div class="naver" style="">
</div>
</div> </div> </div> </div>
2. 아래 내용중 제목은 가져왔는데
subject = soup.select_one('h3[style="font-size:25px;"]').text
select_one 으로 아래 갈비, 슬립퍼를 따로 가져오고 싶습니다.
감사합니다.
<h3 style="font-size:25px;" class="mt-2"><b>제목입니다.</b></h3>
<p class="mt-2" style="padding-bottom:15px;font-size:12px;border-bottom:1px solid #393c4a"> <span class="badge badge-danger float-left " style="font-size:100%;font-size:10px;margin-top:1px;">19세</span>
<img src="/style/img/aaaa.png" alt="..." class="rounded" style="width:16px;margin-top:-3px;">
갈비
작가 : 슬립퍼<span class="float-right"><span style="font-size:13px;" class="float-right"><!--<i class="fa fa-eye float"></i> 480146--></span></span>
</p>
답변 2
아래의 코드를 참고해 보시겠어요~
from bs4 import BeautifulSoup
html = """
<div class="container mt-2">
<div class="row list-item">
<div class="col-12 col-sm-7 col-md-7 mt-2">
<div class="card card-fluid">
<script>
document.write('<img src="'+img_domain+'2021/0910/202109101125338484700.jpg'+'" style="width:100%">');
</script>
<img src="https://website.com/2021/0910/202109101125338484700.jpg" style="width:100%">
<div class="naver" style="">
</div>
</div> </div> </div> </div>
"""
soup = BeautifulSoup(html, 'html.parser')
# 이미지 URL 가져오기
image_url = soup.select_one('img')['src']
print(image_url)
subject = soup.select_one('h3[style="font-size:25px;"]').text
# 작가 정보 가져오기
author_info = soup.select_one('p').text
# 갈비와 슬립퍼 가져오기
elements = author_info.split("작가 : ")
book_title = elements[0].strip()
author = elements[1].strip()
print(book_title)
print(author)
# pip install beautifulsoup4
import re
from bs4 import BeautifulSoup
src = """
<div class="container mt-2">
<div class="row list-item">
<div class="col-12 col-sm-7 col-md-7 mt-2">
<div class="card card-fluid">
<script>
document.write('<img src="'+img_domain+'2021/0910/202109101125338484700.jpg'+'" style="width:100%">');
</script>
<img src="https://website.com/2021/0910/202109101125338484700.jpg" style="width:100%">
<div class="naver" style="">
</div>
</div> </div> </div> </div>
"""
soup = BeautifulSoup(src, 'html.parser')
data = soup.select_one('img[src]')
print('1.')
print(data['src'])
src = """
<h3 style="font-size:25px;" class="mt-2"><b>제목입니다.</b></h3>
<p class="mt-2" style="padding-bottom:15px;font-size:12px;border-bottom:1px solid #393c4a"> <span class="badge badge-danger float-left " style="font-size:100%;font-size:10px;margin-top:1px;">19세</span>
<img src="/style/img/aaaa.png" alt="..." class="rounded" style="width:16px;margin-top:-3px;">
갈비
작가 : 슬립퍼<span class="float-right"><span style="font-size:13px;" class="float-right"><!--<i class="fa fa-eye float"></i> 480146--></span></span>
</p>
"""
soup = BeautifulSoup(src, "html.parser")
subject = soup.select_one('h3[style="font-size:25px;"]').text
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/#going-sideways
content = soup.select_one('p[class="mt-2"] img[class="rounded"]').next_sibling
print('2.')
print(content)