-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_scraping_3.py
70 lines (42 loc) · 1.44 KB
/
web_scraping_3.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
from bs4 import BeautifulSoup
import re
# re -> regular expression
# 1 - Changing attributes of a tag
# <option selected="" value="course-type">
with open("index2.html", "r") as f:
doc = BeautifulSoup(f, "html.parser")
tag = doc.find("option")
tag['value'] = 'new-course'
tag['selected'] = 'false'
# print(tag)
# 2 - Add an attribute to a tag and assign a value
tag['rating'] = '15'
# print(tag)
# 3 - Display all attributes of a tag
# print(tag.attrs)
# 4 - Find multiple tags
tags = doc.find_all(["p", "div", "li"])
# print(tags)
# 5 - Find tag with certain text
# Option tag having a certain text
# <option value="diploma">Diploma</option>
tag1 = doc.find_all(["option"], string="Diploma")
# print(tag1)
# 6 - Also find an attribute paired with a tag
# <option value="certificate">Certificate</option>
tag2 = doc.find_all(["option"], string="Certificate", value="certificate")
# print(tag2)
# 7 - Find different class names
tag3 = doc.find_all(class_ = 'btn-item')
# print(tag3)
# 8 - Find using regular expression (import re)
# Matching the Dollar Sign "$" and anything that comes after the dollar sign (\ used because $ sign is used)
# Eg: $100, $hello, etc.
tag4 = doc.find_all(string=re.compile("\$.*"))
# print(tag4)
# Make the above in a pretier format by removing spacing
for val in tag4:
print(val.strip())
# 9 - Limit the number of results in a search
tag5= doc.find_all(string=re.compile("course.*"), limit=2)
print(tag5)