forked from kudeh/automate-the-boring-stuff-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurllink2.py
More file actions
35 lines (29 loc) · 849 Bytes
/
Copy pathurllink2.py
File metadata and controls
35 lines (29 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# To run this, download the BeautifulSoup zip file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
count = 0
total_sum = 0
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
# Retrieve all of the anchor tags
tags = soup('span')
for tag in tags:
count+=1
# Look at the parts of a tag
print('TAG:', tag)
print('URL:', tag.get('href', None))
item = tag.contents[0]
value = int(item)
print('Contents:', item)
print('Attrs:', tag.attrs)
total_sum += value
print('Count: ' + str(count))
print('Sum: ' + str(total_sum))