Я попытался удалить таблицу с BeautifulSoup
, и из 4 моих попыток первые 3 не работают, я не знаю почему!
В четвертом подходе я попытался использовать pandas
, но результаты больше не указываются c.
import requests
import bs4
res = requests.get(
"https://www.moneycontrol.com/stocks/marketstats/industry-classification/bse/aerospace-defence.html")
soup = bs4.BeautifulSoup(res.text, 'lxml')
# 1st try by copy selector from inspect element
table = soup.find_all(
'#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm.PR.Ohidden')
print(table)
# 2nd try by specifically writing class by attribute method
table = soup.find_all(
'div', attrs={'class': 'bsr_table.hist_tbl_hm.PR.Ohidden'})
print(table)
# 3rd conventional style
table = soup.find('table')
table_rows = table.find('tr')
for tr in table_rows:
td = tr.find_all('td')
row = [i.text() for i in td]
print(td)
import pandas as pd
# 4th by pandas
dfs = pd.read_html(
'https://www.moneycontrol.com/stocks/marketstats/industry-classification/bse/aerospace-defence.html')
for df in dfs:
print(df)
Вывод, который я получил:
0 Hindustan Aeron Add to Watchlist | Portfolio... ... 627.53
1 5-Day ... NaN
2 10-Day ... NaN
3 30-Day ... NaN
4 3-Day ... NaN
5 5-Day ... NaN
6 8-Day ... NaN
7 TAAL Enterprise Add to Watchlist | Portfolio... ... 135.34
8 5-Day ... NaN
9 10-Day ... NaN
10 30-Day ... NaN
11 3-Day ... NaN
12 5-Day ... NaN
13 8-Day ... NaN
14 Taneja Aerospac Add to Watchlist | Portfolio... ... 21.76
15 5-Day ... NaN
16 10-Day ... NaN
17 30-Day ... NaN
18 3-Day ... NaN
19 5-Day ... NaN
20 8-Day ... NaN
Но вывод, который я хочу:
is a data frame having columns 1)Open 2) High 3) Low 4) Price 5)Current Price 6) Percent change 7) Sector:- aerospace defence.
Спасибо, что ответили на сомнение и способствовали ему.