title: Python - BeautifulSoup: Pull stock data from Morningstar
body: <p>I am trying to extract the two data points from Morning Star website for a list of companies and save it to a text file, but I am not sure how to approach this task. Below is my code:</p>
  <pre><code>from bs4 import BeautifulSoup as BS
  
  thislist = [&quot;AAPL&quot;,&quot;FB&quot;,&quot;TSLA&quot;,&quot;DIS&quot;] 
  for symbol in thislist:
      print ('Getting data for ' + symbol + '...\n')
  
  # extract from this website
  url=&quot;https://www.morningstar.com/stocks/xnas/&quot; + symbol + &quot;/quote&quot;
          
  soup = BS(url)
          
  # Find the Value of Last Close Price
  for text in soup.find_all('div class', name_='Last Close'):
      Last_Close = text.find_all('dp-value price-down')
      print(Last_Close)     
          
  # Find the Value of its Market Cap
  for text in soup.find_all('div class', name_='Market Cap'):
      Market_Cap = text.find_all('dp-value')
      print(Market_Cap)      
          
  # Print the table
  print(symbol, Last_Close, Market_Cap)
              
  # Save the data in a .txt file
  df.to_csv(r'c:\data\testing.txt', header=None, index=None, sep=' ', mode='a')
  </code></pre>