<jupyter_start><jupyter_text>Parsing with Beautiful soupBeautifulsoup is already installed in the Anaconda package<jupyter_code># rewritten the code for Python 3... if fetches anchor tags

import urllib.request
from bs4 import BeautifulSoup

url = input('Enter - ')

html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'lxml')

# Retrieve a list of anchor tags
# Each tag is like a dictionary of HTML attributes

tags = soup('a')
for tag in tags:
    print(tag.get('href', None))
    
# test on this page: http://www.dr-chuck.com/page1.htm<jupyter_output><empty_output><jupyter_text>Doing the first assignment<jupyter_code>import urllib.request
from bs4 import BeautifulSoup

url = 'http://python-data.dr-chuck.net/comments_371514.html'

html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'lxml')

# Retrieve a list of anchor tags
# Each tag is like a dictionary of HTML attributes

tags = soup('span')
#print(tags)

sum_of_tags = 0

for tag in tags:
    sum_of_tags += int(tag.contents[0])
    
print(sum_of_tags)
# Example code to retrieve information

# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
    # Look at the parts of a tag
    print('TAG:',tag)
    print('URL:',tag.get('href', None))
    print('Contents:',tag.contents[0])
    print('Attrs:',tag.attrs)
    
def adding_series():
    summ = 0
    for i in range(11):
        summ += i
    return summ

adding_series()
# Making a function out of the assignment
import urllib.request
from bs4 import BeautifulSoup

url = 'http://python-data.dr-chuck.net/comments_371514.html'

def assign1(url):
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'lxml')
    tags = soup('span')
    
    sum_of_tags = 0
    for tag in tags:
        sum_of_tags += int(tag.contents[0])
    return sum_of_tags
    
assign1(url)<jupyter_output><empty_output><jupyter_text>Doing the second assignment<jupyter_code># training

import urllib.request
from bs4 import BeautifulSoup

seed_url = 'http://python-data.dr-chuck.net/known_by_Fikret.html'
urls = [seed_url]

position = 3
count = 4

pos = position - 1

for i in range(count):
    html = urllib.request.urlopen(urls[-1]).read()
    soup = BeautifulSoup(html, 'lxml')

    tags = soup('a')
    urls.append(tags[pos].get('href', None))

print(urls)
# solution

import urllib.request
from bs4 import BeautifulSoup

seed_url = 'http://python-data.dr-chuck.net/known_by_Alan.html '
urls = [seed_url]

position = 18
count = 7

pos = position - 1

for i in range(count):
    html = urllib.request.urlopen(urls[-1]).read()
    soup = BeautifulSoup(html, 'lxml')

    tags = soup('a')
    urls.append(tags[pos].get('href', None))

print(urls[-1])<jupyter_output>http://python-data.dr-chuck.net/known_by_Shinay.html
{'fraction_non_alphanumeric': 0.12733812949640289, 'fraction_numerical': 0.014028776978417265, 'mean_word_length': 4.056363636363637, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 5}, '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': '409124', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 998, 'n_words': 248, 'path': 'notebooks/programs_that_surf.ipynb', 'repo_name': 'johanfrisk/Python_at_web', 'size': '2780'}