<jupyter_start><jupyter_text>`find` and `find_all` continuing with our given set of html<jupyter_code>html = """
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>First HTML Page</title>
</head>
<body>
  <div>
    <h3 data-example="yes">hi</h3>
    <p>more text.</p>
  </div>
  <ol>
    <li class="special">This list item is special.</li>
    <li class="special" id="first">This list item is also very special.</li>
    <li>This list item is not special.</li>
  </ol>
  <div data-example="yes">bye</div>
</body>
</html>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
print(soup)<jupyter_output><!DOCTYPE html>

<head>
<meta charset="utf-8"/>
<title>First HTML Page</title>
</head>
<body>
<div>
<h3 data-example="yes">hi</h3>
<p>more text.</p>
</div>
<ol>
<li class="special">This list item is special.</li>
<li class="special" id="first">This list item is also very special.</li>
<li>This list item is not special.</li>
</ol>
<div data-example="yes">bye</div>
</body><jupyter_text>Identify  by using `find` and `find_all`<jupyter_code>print(soup.find("div"))<jupyter_output><div>
<h3 data-example="yes">hi</h3>
<p>more text.</p>
</div><jupyter_text>`find` only returns the first one that matches the requirement<jupyter_code>d_find = soup.find("div")
d_find<jupyter_output><empty_output><jupyter_text>**`find_all`** returns all elements that matches the requirement in list<jupyter_code>d_all = soup.find_all("div")
d_all
# returning second <div> element
print(d_all[1])<jupyter_output><div data-example="yes">bye</div><jupyter_text>**ex)** find tag "li"<jupyter_code>li = soup.find("li")
li
li_all = soup.find_all("li")
li_all<jupyter_output><empty_output><jupyter_text>other usage of `find` and `find_all` **id**<jupyter_code>id_first = soup.find(id="first")
id_first<jupyter_output><empty_output><jupyter_text>**class**   note that if certain tag, command already exists in python, add underbar ( _ ) to avoid overlapping<jupyter_code>class_special = soup.find_all(class_="special")
class_special<jupyter_output><empty_output><jupyter_text>**attrs**  attribution<jupyter_code>attrs = soup.find_all(attrs={"data-example": "yes"})
attrs<jupyter_output><empty_output><jupyter_text>**Other example of navigating with `find`**<jupyter_code>soup.find('h3')
soup.find("h3")["data-example"]<jupyter_output><empty_output><jupyter_text>`select` and `select_all`<jupyter_code>html = """
<!DOCTYPE html>
<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="special">This list item is special.</li>
    <li class="special">This list item is also very special.</li>
    <li>This list item is not special.</li>
  </ol>
  <div data-example="yes">bye</div>
</body>
</html>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
print(soup)<jupyter_output><!DOCTYPE html>

<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="special">This list item is special.</li>
<li class="special">This list item is also very special.</li>
<li>This list item is not special.</li>
</ol>
<div data-example="yes">bye</div>
</body><jupyter_text>**id**<jupyter_code># return as string in list
id = soup.select("#first")
id
id_2 = soup.select("#first")[0]
id_2<jupyter_output><empty_output><jupyter_text>**attrs**return all by attributs<jupyter_code>attrs = soup.select("[data-example]")
attrs
attrs[0]
attrs[1]
attrs[2]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18512396694214875, 'fraction_numerical': 0.006887052341597796, 'mean_word_length': 6.24750499001996, 'pattern_counts': {'":': 1, '<': 152, '<?xml version=': 0, '>': 152, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '72949', 'license': 'MIT', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1383, 'n_words': 296, 'path': '2_python_Packages_Libraries/5_beautifulSoup/01_reviewNote_BS/02_find_select.ipynb', 'repo_name': 'lucaseo/TIL', 'size': '3630'}