-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb2.py
More file actions
33 lines (27 loc) · 807 Bytes
/
web2.py
File metadata and controls
33 lines (27 loc) · 807 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
import requests
import pandas as pd
from bs4 import BeautifulSoup
page = requests.get('https://www.moneycontrol.com/markets/indian-indices/')
soup = BeautifulSoup(page.content, 'html.parser')
divisions = soup.find(class_='responsive')
tr = divisions.find('tr')
#for heading
heads = tr.find_all('th')
heading = [head.get_text() for head in heads]
#for values
tbody = divisions.find('tbody')
tr_bodys = tbody.find_all('tr')
values = [tr_body.get_text() for tr_body in tr_bodys]
##print(values)
val = []
for value in values:
value = list(value.split('\n'))
while '' in value:
value.remove('')
val.append(value)
##print(heading)
##for each in val:
## print(each)
df = pd.DataFrame(data = val, columns=heading)
df.to_csv('data2.csv')
print(df)