Yes, you can find "Section 2" by "Unit 1" and "Section 1" in Python by using the BeautifulSoup library. Here's an example code to achieve this:

from bs4 import BeautifulSoup

html_code = """
<ol>
<li>Unit 1<ul>
<li>Section 1<ol>
<li>Section 2</li>
</ol>
</li>
</ul>
</li>
<li>Unit 2<ul>
<li>Section 3</li>
</ul>
</li>
<li>Unit 3<ol>
<li>Section 4</li>
</ol>
</li>
<li>Unit 4</li>
</ol>
"""

soup = BeautifulSoup(html_code, 'html.parser')

# Find the "Unit 1" element
unit_1 = soup.find(text="Unit 1")

# Find the "Section 1" element under "Unit 1"
section_1 = unit_1.find_next(text="Section 1")

# Find the "Section 2" element under "Section 1"
section_2 = section_1.find_next(text="Section 2")

print(section_2)

Output:

Section 2

In the above code, we use BeautifulSoup to parse the HTML code. We first find the "Unit 1" element using soup.find(text="Unit 1"). Then, we find the "Section 1" element under "Unit 1" using unit_1.find_next(text="Section 1"). Finally, we find the "Section 2" element under "Section 1" using section_1.find_next(text="Section 2")

HTML codeolliUnit 1ulliSection 1olliSection 2liolliulliliUnit 2ulliSection 3liulliliUnit 3olliSection 4liolliliUnit 4liolAbove is the HTML code can I find Section 2 by Unit 1 and Section 1 in Python

原文地址: http://www.cveoy.top/t/topic/h0pp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录