<jupyter_start><jupyter_text>bs4 tutorial  html is a markup languange<jupyter_code>html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""<jupyter_output><empty_output><jupyter_text>By changing cell from "Code" to "Markdown", we can see how browsers render html_doc. Double click next cell to see that is exaclty html_doc: The Dormouse's storyThe Dormouse's storyOnce upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well....  How can extract data from html? bs4 is a library that "parses" html.It allows to immediately find data by using the html tags that contain what we are looking for<jupyter_code>from bs4 import BeautifulSoup<jupyter_output><empty_output><jupyter_text>Step 1. Create a BeautifulSoup Object from a str<jupyter_code>soup = BeautifulSoup(html_doc)<jupyter_output><empty_output><jupyter_text>Step 2. Get the first linkLink are contained in "a" tags<jupyter_code>link = soup.find("a")
print(link.text)
print(link["href"])<jupyter_output>http://example.com/elsie<jupyter_text>Step 3. Get all the links<jupyter_code>links = soup.find_all("a")
print(links)
[l.text for l in links]
link_texts = list()

for l in links:
    link_texts.append(l.text)
link_texts<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12897628687102372, 'fraction_numerical': 0.005205320994794679, 'mean_word_length': 6.330508474576271, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '430632', 'license': 'MIT', 'n_tokens_mistral': 614, 'n_tokens_neox': 601, 'n_words': 178, 'path': 'What we did in class/CarrefourToVivino/1 - bs4 tutorial.ipynb', 'repo_name': 'ElLorans/PythonCrashCourse', 'size': '1729'}