-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.py
More file actions
54 lines (39 loc) · 1.52 KB
/
18.py
File metadata and controls
54 lines (39 loc) · 1.52 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# kinds of filters ---
import re
from bs4 import BeautifulSoup as bs
html = '''<html><p>Top Three</p><p><pre>Programming Languages are:</pre></p><p><b>Java, Python, Cplusplus</b></p>'''
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>
"""
soup = bs(html, 'html.parser')
soup2 = bs(html_doc, 'html.parser')
# find_all
print(soup.find_all('p'))
# regular expression ---
# gives all the tag which starts with 'p'
for tag in soup.find_all(re.compile('^p')):
print(tag.name)
# gives all the tag which contain the letter 't'
for tag in soup.find_all(re.compile('t')):
print(tag.name)
# list ---
for tag in soup.find_all(["a", "p"]): # finds all the a tag or p tag in the soup
print(tag.name) # p p p
# true ---
for t in soup.find_all(True): # finds all the tags in the soup
print(t.name)
# for x in soup.find_all(True): print(x)
# functions ---
def has_class_but_no_id(tag):
"Returns the tag which contain the class attribute in them but not the id "
return tag.has_attr('class') and not tag.has_attr('id')
for f in soup2.find_all(has_class_but_no_id):
print(f)