<jupyter_start><jupyter_text>[![AnalyticsDojo](https://github.com/rpi-techfundamentals/spring2019-materials/blob/master/fig/final-logo.png?raw=1)](http://rpi.analyticsdojo.com)Introduction to Python - Web Miningintroml.analyticsdojo.com  Web Mining  This tutorial is directly from the the BeautifulSoup documentation.[https://www.crummy.com/software/BeautifulSoup/bs4/doc/] Before you beginIf running locally you need to make sure that you have beautifulsoup4 installed. `conda install beautifulsoup4` It should already be installed on colab.   All html documents have structure.  Here, we can see a basic html page.<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>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....<jupyter_code>from bs4 import BeautifulSoup
import requests
soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())<jupyter_output><empty_output><jupyter_text>A Retreived Beautiful Soup Object - Can be parsed via dot notation to travers down the hierarchy by *class name*, *tag name*, *tag type*, etc.<jupyter_code>soup
#Select the title class.
soup.title
 

#Name of the tag.
soup.title.name



#String contence inside the tag
soup.title.string



#Parent in hierarchy.
soup.title.parent.name



#List the first p tag.
soup.p



#List the class of the first p tag.
soup.p['class']



#List the class of the first a tag.
soup.a



#List all a tags.
soup.find_all('a')



soup.find(id="link3")

#The Robots.txt listing who is allowed.
response = requests.get("https://en.wikipedia.org/robots.txt")
txt = response.text
print(txt)
response = requests.get("https://www.rpi.edu")
txt = response.text
soup = BeautifulSoup(txt, 'html.parser')

print(soup.prettify())
soup.find_all('a')
# Experiment with selecting your own website.  Selecting out a url. 

response = requests.get("enter url here")
txt = response.text
soup = BeautifulSoup(txt, 'html.parser')

print(soup.prettify())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13237577639751552, 'fraction_numerical': 0.005046583850931677, 'mean_word_length': 5.254854368932039, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '111415', 'license': 'MIT', 'n_tokens_mistral': 898, 'n_tokens_neox': 864, 'n_words': 248, 'path': 'site/_build/html/_sources/notebooks/03-viz-api-scraper/02-intro-python-webmining.ipynb', 'repo_name': 'rpi-techfundamentals/spring2020_website', 'size': '2576'}