[Python 3] Questions about Beautiful Soup

Asked 2 years ago, Updated 2 years ago, 19 views

in Beautiful Soup
<div class="hoge1">
 <div class="hoge2">
  <p>hogehoge</p>
 </div>
</div>

How do I get the <p> part from HTML code like this?
 

python

2022-09-29 22:34

1 Answers

How about the following?

 from bs4 import BeautifulSoup
US>"html=""
    <div class="hoge1">
     <div class="hoge2">
      <p>hogehoge1</p>
      <p>hogehoge2</p>
     </div>
    </div>
"""
soup = BeautifulSoup(html, "lxml")
text1=soup.find_all("p")#Outputs a tag of p from html in an array
text2=text1[0].string#Retrieves tag string from array data retrieved
text3=text1[1].string#Retrieves tag string from array data retrieved

print(text1)
print(text2)
print(text3)

>> [<p>hogehoge1</p>, <p>hogehoge2</p>]
>> hogehoge1
>> hogehoge2


2022-09-29 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.