-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (49 loc) · 1.4 KB
/
main.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
# Web Scraper
# Libraries Used :
# 1. requests ( pip install requests )
# 2. bs4 ( pip install bs4 )
# 3. html5lib ( pip install html5lib
import requests
from bs4 import BeautifulSoup
# url = "your url here"
url = "https://www.moneycontrol.com/stocksmarketsindia/"
# Step 1 : Get the HTML
r = requests.get(url)
htmlContent = r.content
# print(htmlContent)
# Step 2 : Parse the HTML
soup = BeautifulSoup(htmlContent, 'html.parser')
# print(soup.prettify)
# Step 3 : HTML tree traversal
# The four major and important objects are :
# 1. BeautifulSoup --> print(type(soup))
# 2. Tag --> print(type(title))
# 3. NavigableString --> print(type(title.string))
# 4. Comments
# Get the Title of the Page
title = soup.title
# print((title.string))
# Get all meta tags
metaTags = soup.find_all('meta')
# print(metaTags)
# Get all p tags
pTags = soup.findAll('p')
# print(pTags)
# Get first element in HTML tag
# print(soup.find('p'))
# Get class of any element in HTML page
# print(soup.find('p')['class'])
# print(soup.find('p').get_text())
# print(soup.get_text())
# Get all a tags
anchorTags = soup.find_all('a')
# print(anchorTags)
# for links in anchorTags:
# print(links.get('href'))
# Get all images on the HTML page
imgs = soup.find_all('img')
# print(imgs)
# for srcs in imgs:
# print(srcs.get('src'))
# for more / advanced scraping
# go to : https://www.crummy.com/software/BeautifulSoup/bs4/doc/