-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
38 lines (30 loc) · 1.32 KB
/
15.py
File metadata and controls
38 lines (30 loc) · 1.32 KB
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
36
37
38
# parent element in soup
from bs4 import BeautifulSoup as bs
html = """<html><head><title>Tutorials Point</title></head>
<body>
<p class="title"><b>The Biggest Online Tutorials Library, It's all Free</b></p>
<p class="prog">Top 5 most used Programming Languages are:
<a href="https://www.tutorialspoint.com/java/java_overview.htm" class="prog" id="link1">Java</a>,
<a href="https://www.tutorialspoint.com/cprogramming/index.htm" class="prog" id="link2">C</a>,
<a href="https://www.tutorialspoint.com/python/index.htm" class="prog" id="link3">Python</a>,
<a href="https://www.tutorialspoint.com/javascript/javascript_overview.htm" class="prog" id="link4">JavaScript</a> and
<a href="https://www.tutorialspoint.com/ruby/index.htm" class="prog" id="link5">C</a>;
as per online survey.</p>
<p class="prog">Programming Languages</p>
"""
soup = bs(html, 'html.parser')
tag = soup.title
print(tag)
print(tag.parent) # .parent gives us the parent tag
print(tag.parent.name) # gives the name of the parent
html_tag = soup.html
# the parent of html_TAg is the bs4 object itself----
print(type(html_tag.parent)) # <class 'bs4.BeautifulSoup'>
# the parent of beautiful soup object is none
print(soup.parent)
# iterating over the parents
for parent in soup.a.parents:
if parent is not None:
print(parent.name)
else:
print(parent)