-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlParse.py
More file actions
34 lines (30 loc) · 1012 Bytes
/
htmlParse.py
File metadata and controls
34 lines (30 loc) · 1012 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
# 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
import re
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ') # http://py4e-data.dr-chuck.net/comments_2381078.html
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
numberOfSpanTags = 0
total = 0
# Retrieve all of the anchor tags
tags = soup('span')
for tag in tags:
# Look at the parts of a tag
numberOfSpanTags = numberOfSpanTags + 1
numbers = re.findall("[0-9]+", tag.contents[0])
print('TAG:', tag)
print('URL:', tag.get('href', None))
print('Contents:', tag.contents[0])
print('Attrs:', tag.attrs)
for number in numbers:
total = total + int(number)
print('Number of span tags:', numberOfSpanTags)
print('Total:', total)