<jupyter_start><jupyter_text>Navigating Beautiful Soup<jupyter_code>from bs4 import BeautifulSoup
html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>First HTML Page</title>
</head>
<body>
  <div id="first">
    <h3 data-example="yes">hi</h3>
    <p>more text.</p>
  </div>
  <ol>
    <li class="super-special">This list item is special.</li>
    <li>This list item is not special.</li>
    <li class="special">This list item is also very special.</li>
  </ol>
  <div data-example="yes">bye</div>
</body>
</html>
"""
soup = BeautifulSoup(html, "html.parser")<jupyter_output><empty_output><jupyter_text>how to nagivate the `soup`?**via Tags**- parents / parents- contents- next_siblings / next_siblings- previous_sibling / previous_siblings**via Searching**- find_parent / find_parents- find_next_sibling / find_next_siblings  `contents` via Tags- returns all contents within each Tags as a list - elements are seperated with `\n`<jupyter_code>data = soup.body.contents
print(data)
# the first element[0] is is '\n'. so we go for [1]
data = soup.body.contents[1] 
print(data)
data = soup.body.contents[1].contents[1] 
print(data)
data['data-example']<jupyter_output><empty_output><jupyter_text>via Searching- ex)  and  are sibling Tags (sibling)- They are on the same level  `next_sibling`<jupyter_code>data_2 = soup.body.contents[1]
print(data_2)
data_2 = soup.body.contents[1].next_sibling.next_sibling
print(data_2)<jupyter_output><ol>
<li class="super-special">This list item is special.</li>
<li>This list item is not special.</li>
<li class="special">This list item is also very special.</li>
</ol><jupyter_text>**`find_next_sibling`** skips '/n' newline<jupyter_code>data = soup.find(id = "first")
print(data)
data = soup.find(id = "first").find_next_sibling()
print(data)
data = soup.find(id = "first").find_next_sibling().find_next_sibling()
print(data)<jupyter_output><div data-example="yes">bye</div><jupyter_text>**`find_previous_sibling`**<jupyter_code>data = soup.select("[data-example]")[1].find_previous_sibling()
print(data)
#find previous sibling with certain condition
data = soup.select("[data-example]")[1].find_previous_sibling()
print(data)<jupyter_output><ol>
<li class="super-special">This list item is special.</li>
<li>This list item is not special.</li>
<li class="special">This list item is also very special.</li>
</ol><jupyter_text>`parent`<jupyter_code>data_3 = soup.find(class_="super-special")
print(data_3)
data_3 = soup.find(class_="super-special").parent
print(data_3)
data_3 = soup.find(class_="super-special").parent.parent
print(data_3)<jupyter_output><body>
<div id="first">
<h3 data-example="yes">hi</h3>
<p>more text.</p>
</div>
<ol>
<li class="super-special">This list item is special.</li>
<li>This list item is not special.</li>
<li class="special">This list item is also very special.</li>
</ol>
<div data-example="yes">bye</div>
</body><jupyter_text>**`find_parent`** skips '\n' newline<jupyter_code>data = soup.find("h3").find_parent()
print(data)
# find parent with certain condition
data = soup.find("h3").find_parent("body")
print(data)<jupyter_output><body>
<div id="first">
<h3 data-example="yes">hi</h3>
<p>more text.</p>
</div>
<ol>
<li class="super-special">This list item is special.</li>
<li>This list item is not special.</li>
<li class="special">This list item is also very special.</li>
</ol>
<div data-example="yes">bye</div>
</body>
{'fraction_non_alphanumeric': 0.17482517482517482, 'fraction_numerical': 0.00844988344988345, 'mean_word_length': 6.078350515463917, 'pattern_counts': {'":': 0, '<': 104, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '155413', 'license': 'MIT', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1270, 'n_words': 281, 'path': '2_python_Packages_Libraries/5_beautifulSoup/01_reviewNote_BS/04_Navigations.ipynb', 'repo_name': 'lucaseo/TIL', 'size': '3432'}