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

python select_one 질문드립니다. 채택완료

망고프리 2년 전 조회 2,069

안녕하세요.

1. python soup.select_one('') 으로 아래 적색부분 url을 가져올려고 하는데 

https://website.com/2021/0910/202109101125338484700.jpg

어떻게 가져와야 되는지

 

   

       https://website.com/2021/0910/202109101125338484700.jpg" style="width:100%">         

 
 
  
  
  

 

2. 아래 내용중 제목은 가져왔는데

subject             = soup.select_one('h3[style="font-size:25px;"]').text

select_one 으로 아래 갈비, 슬립퍼를 따로 가져오고 싶습니다.

감사합니다.

 

제목입니다.

      

          19세           ...                 갈비                                                     작가 : 슬립퍼             

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

답변 2개

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

아래의 코드를 참고해 보시겠어요~

 

 

from bs4 import BeautifulSoup

html = """

   

       https://website.com/2021/0910/202109101125338484700.jpg" style="width:100%">        

 
 
 
"""

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)  

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

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

2년 전

</p>

<p># pip install beautifulsoup4</p>

<p> </p>

<p>import re

from bs4 import BeautifulSoup</p>

<p> </p>

<p> </p>

<p>src = """

<div class="container mt-2">

<div class="row list-item"></p>

<p><div class="col-12 col-sm-7 col-md-7 mt-2"></p>

<p><div class="card card-fluid">

    <script>

            document.write('<img src="'+img_domain+'2021/0910/202109101125338484700.jpg'+'" style="width:100%">');

      </script></p>

<p>       <img src="<a href="https://website.com/2021/0910/202109101125338484700.jpg"" target="_blank" rel="noopener noreferrer">https://website.com/2021/0910/202109101125338484700.jpg"</a> style="width:100%">

        <div class="naver" style=""></p>

<p>         </div>

  </div> </div>   </div>   </div>  

"""</p>

<p>soup = BeautifulSoup(src, 'html.parser')

data = soup.select_one('img[src]')</p>

<p> </p>

<p>print('1.')

print(data['src'])</p>

<p> </p>

<p> </p>

<p>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>

"""</p>

<p>soup = BeautifulSoup(src, "html.parser")

subject             = soup.select_one('h3[style="font-size:25px;"]').text</p>

<p># <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#going-sideways" target="_blank" rel="noopener noreferrer">https://www.crummy.com/software/BeautifulSoup/bs4/doc/#going-sideways</a>

content             = soup.select_one('p[class="mt-2"] img[class="rounded"]').next_sibling</p>

<p> </p>

<p>print('2.')

print(content)</p>

<p>

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

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

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

로그인