skip to Main Content

Here is my script….

from bs4 import BeautifulSoup
import pandas as pd
import requests


url = f"https://niftyinvest.com/put-call-ratio/ACC"
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
for data in soup.find_all("p"):
    print(data.get_text())

My output is as follows….

Nifty Invest

1857
arrow_drop_up

                  7.00 (0.38%)


Related Pages for ACC
Last Updated on
                        16 Jun 2023 04:15 PM
                         IST  

On  28 Apr  
                        ACC was trading at
                        1762.85  with pcr
                        value  1.5

On  2 May  
                        ACC was trading at
                        1766  with pcr
                        value  0.6

On  3 May  
                        ACC was trading at
                        1742  with pcr
                        value  0.82

On  4 May  
                        ACC was trading at
                        1771  with pcr
                        value  0.71

On  5 May  
                        ACC was trading at
                        1762  with pcr
                        value  0.67

On  8 May  
                        ACC was trading at
                        1765  with pcr
                        value  0.79

On  9 May  
                        ACC was trading at
                        1757  with pcr
                        value  1.17

On  10 May  
                        ACC was trading at
                        1752  with pcr
                        value  1.07

On  11 May  
                        ACC was trading at
                        1790  with pcr
                        value  0.79

On  12 May  
                        ACC was trading at
                        1805  with pcr
                        value  0.48

On  15 May  
                        ACC was trading at
                        1804.95  with pcr
                        value  0.71

On  16 May  
                        ACC was trading at
                        1786  with pcr
                        value  0.78

On  17 May  
                        ACC was trading at
                        1757  with pcr
                        value  0.6

On  18 May  
                        ACC was trading at
                        1712  with pcr
                        value  0.74

On  19 May  
                        ACC was trading at
                        1729  with pcr
                        value  0.86

On  22 May  
                        ACC was trading at
                        1804.4  with pcr
                        value  0.56

On  24 May  
                        ACC was trading at
                        1783  with pcr
                        value  0.55

On  25 May  
                        ACC was trading at
                        1783  with pcr
                        value  0.58

On  26 May  
                        ACC was trading at
                        1787.95  with pcr
                        value  0.57

On  29 May  
                        ACC was trading at
                        1794  with pcr
                        value  0.57

On  30 May  
                        ACC was trading at
                        1799  with pcr
                        value  0.55

On  31 May  
                        ACC was trading at
                        1774.9  with pcr
                        value  0.61

On  1 Jun  
                        ACC was trading at
                        1799.9  with pcr
                        value  0.58

On  2 Jun  
                        ACC was trading at
                        1815  with pcr
                        value  0.59

On  5 Jun  
                        ACC was trading at
                        1812.5  with pcr
                        value  0.6

On  6 Jun  
                        ACC was trading at
                        1857  with pcr
                        value  0.65

On  7 Jun  
                        ACC was trading at
                        1855.1  with pcr
                        value  0.63

On  8 Jun  
                        ACC was trading at
                        1834  with pcr
                        value  0.6

On  9 Jun  
                        ACC was trading at
                        1833.05  with pcr
                        value  0.64

On  12 Jun  
                        ACC was trading at
                        1837  with pcr
                        value  0.65

On  13 Jun  
                        ACC was trading at
                        1843.9  with pcr
                        value  0.67

On  14 Jun  
                        ACC was trading at
                        1853  with pcr
                        value  0.64

On  15 Jun  
                        ACC was trading at
                        1847.5  with pcr
                        value  0.6

On  16 Jun  
                        ACC was trading at
                        1857  with pcr
                        value  0.63

But Here I want my output as….

28 Apr     ACC        1762.85           1.50
2 May      ACC        1766.00           0.60
3 May      ACC        1742.00           0.82
4 May      ACC        1771.00           0.71
5 May      ACC        1762.00           0.67

How can I get that data for all dates present in my script output.

2

Answers


  1. The text you want is in nested <b> elements in each <p>, e.g.

    <p>On&nbsp;&nbsp;<span><b
                    class="blue-text text-darken-2">28 Apr</b></span>&nbsp;&nbsp;
        ACC was trading at
        <b class="blue-text text-darken-2">1762.85</b>&nbsp;&nbsp;with pcr
        value&nbsp;&nbsp;<b
                class="blue-text text-darken-2">1.5</b>
    </p>
    

    You can select those elements instead of taking the text of the entire paragraph.

    To format it as a nice table, the easiest way is to put it into a pandas dataframe and print that.

    rows = []
    for p in soup.find_all('p'):
        items = p.find_all('b')
        if items:
            rows.append((items[0].text, 'ACC', *(i.text for i in items[1:])))
    
    df = pd.DataFrame(rows, columns=('Date', 'Symbol', 'Trading At', 'PCR Value'))
    print(df)
    
    Login or Signup to reply.
  2. Here is a working example, please notice that on tokens you have all the control to do what you want 🙂

    url = f"https://niftyinvest.com/put-call-ratio/ACC"
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    
    for data in soup.find_all("p"):    
        text=data.get_text("|",strip=True)  
        tokens=text.split("|")
        if(len(tokens)==6):        
            formatted=tokens[1]+' ' + tokens[2][0:3] + ' '+ ' ' +tokens[3]+ ' '+tokens[5]
            print(formatted)    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search