# 44|PYTHON – Webscraping Introduction

**URL:** <https://www.techhub.training/t/44-python-webscraping-introduction/630>\
**Category:** Challenges\
**Tags:** beginner, python, finance, stocks, webscraping\
**Created:** [May 9, 2023, 1:46am UTC](https://www.techhub.training/t/44-python-webscraping-introduction/630 "2023-05-09T01:46:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Paulsin](https://sea2.discourse-cdn.com/flex016/user_avatar/www.techhub.training/mike_paulsin/32/76_2.png) [@Mike\_Paulsin](https://www.techhub.training/u/Mike_Paulsin)\
**Post date:** [May 9, 2023, 1:46am UTC](https://www.techhub.training/t/44-python-webscraping-introduction/630/1 "2023-05-09T01:46:46Z")

</div>

_BYU Student Author_: @Mike_Paulsin  
_Reviewers_: @Jae, @Nate  
_Estimated Time to Solve:_ 30 - 45 Minutes

We provide the solution to this challenge using:

- Python

Need a program? Click [here](https://www.techhub.training/t/information-about-software-used-on-techhub-training/138).

**Overview**  
Web scraping is the process of extracting data from websites automatically using code, rather than manually copying and pasting data. Python is a popular language for web scraping due to its ease of use, large number of web scraping libraries available, and its ability to handle a wide variety of web data formats.

In this challenge, use Python and the **requests** and **BeautifulSoup** libraries to extract financial data from Yahoo Finance for a given stock ticker symbol. The program should send a GET request to the Yahoo Finance website with the ticker symbol as a parameter in the URL, and then parse the HTML response to extract the desired financial data from the HTML elements on the page.

**Instructions**

1. Ask the user to enter a stock ticker symbol (e.g. AAPL, MSFT, TSLA).
2. Use the **requests** library to send a GET request to the Yahoo Finance website with the ticker symbol as a parameter in the URL. (Remember to include headers in your request!)
3. Parse the HTML response and extract the following financial data:
  - Market open price
  - Previous market close price
  - Market capitalization
  - Price-to-earnings ratio (P/E ratio)
  - Dividend yield (if available)
  - Print out the extracted financial data in a readable format.

> **Example Output**
>
> _Name: Apple Inc.  
> Open: 159.37  
> Close: 157.65  
> Market Cap: 2.544T  
> P/E Ratio: 27.25  
> Dividend Yield: 0.92 (0.58%)_

Good luck and happy coding!

> **Suggestions and Hints**
>
> The URL for the Yahoo Finance page for a given stock ticker symbol looks like this: [Symbol Lookup from Yahoo Finance](https://finance.yahoo.com/quote/)\<TICKER\_SYMBOL\>
> 
> There are a few ways to achieve the desired output. Some possible ways to go about solving this might include the following:
> 
> Use your browser’s developer tools to inspect the page and find the appropriate elements. You can then reference these elements in your python code using xpath.
> 
> Use text parsing to parse out the desired information found in your request. Convert the requested data to a string type. Then using the split function in python, parse out the unnecessary content to get to your desired element.
> 
> Use **BeautifulSoup** and the **get\_text()** method to extract the text content of an HTML element.

**Solution**

> **Solution Code**
>
> ```auto
> user_agent = 'Me' 
> myheaders = {'User-Agent': user_agent}  
> import requests 
> from bs4 import BeautifulSoup 
> 
> # Ask user for stock ticker symbol 
> ticker = input("Enter a stock ticker symbol: ") 
> 
> # Send GET request to Yahoo Finance and parse HTML response 
> url = f"https://finance.yahoo.com/quote/{ticker}" 
> response = requests.get(url,headers=myheaders) 
> soup = BeautifulSoup(response.content, "html.parser") 
> soup = str(soup)  
> 
> # Extract financial data from HTML elements using text parsing 
> name = soup.split('<title>')[1].split('(')[0] 
> open = soup.split('OPEN-value">')[1].split('<')[0] 
> close = soup.split('PREV_CLOSE-value">')[1].split('<')[0] 
> marketcap = soup.split('MARKET_CAP-value">')[1].split('<')[0] 
> PEratio = soup.split('PE_RATIO-value">')[1].split('<')[0] 
> divyield = soup.split('DIVIDEND_AND_YIELD-value">')[1].split('<')[0] 
> 
> #print output 
> print(f"Name: {name}") 
> print(f"Open: {open}") 
> print(f"Close: {close}") 
> print(f"Market Cap: {marketcap}") 
> print(f"P/E Ratio: {PEratio}") 
> print(f"Dividend Yield: {divyield}")
> 
> ```

_Solution Video:_ [Challenge 44|PYTHON – Webscraping Introduction](https://youtu.be/ElShzOA9ZPY)

---

<div class="post-metadata">

**Author:** ![Connor\_Richards](https://sea2.discourse-cdn.com/flex016/user_avatar/www.techhub.training/connor_richards/32/575_2.png) [@Connor\_Richards](https://www.techhub.training/u/Connor_Richards)\
**Post date:** [November 6, 2023, 4:36pm UTC](https://www.techhub.training/t/44-python-webscraping-introduction/630/2 "2023-11-06T16:36:10Z")

</div>

I found financial data for AAPL for this challenge. I don’t have experience with BeautifulSoup, but my code still got the job done. My code and data table output are attached to this comment. Overall, I think it was a great challenge to practice Web scraping with Python. This is what I just learned in class, and this exercise helped me recognize patterns in Yahoo Finance that I can take advantage of when using this source in the future.

 ![Screenshot 2023-11-04 150726](https://us1.discourse-cdn.com/flex016/uploads/weeklytechchallenge/original/2X/7/711d96f11353badc0e0e117cb86dda9af9b5a8e6.png)  
 ![Screenshot 2023-11-04 150735](https://us1.discourse-cdn.com/flex016/uploads/weeklytechchallenge/original/2X/c/ca8c978dfbcea44971da6829478209431828b2cf.png)

---

<div class="post-metadata">

**Author:** ![Noah\_Taylor](https://sea2.discourse-cdn.com/flex016/user_avatar/www.techhub.training/noah_taylor/32/429_2.png) [@Noah\_Taylor](https://www.techhub.training/u/Noah_Taylor)\
**Post date:** [November 21, 2023, 8:10pm UTC](https://www.techhub.training/t/44-python-webscraping-introduction/630/3 "2023-11-21T20:10:04Z")

</div>

I decided to look up Ford’s financial data for this challenge. I am not familiar with BeautifulSoup, but I wrote code to pull up google chrome, type yahoo finance, and look at Fords data. I think this was a great challenge and I highly recommend it.

 ![Python Code](https://us1.discourse-cdn.com/flex016/uploads/weeklytechchallenge/original/2X/c/cd7f183fab103b00661ef3c6d9f607dfff52ebd8.png)  
 ![python chart](https://us1.discourse-cdn.com/flex016/uploads/weeklytechchallenge/original/2X/0/09548d73ad9fb214eb7de3722b55b1e531404266.png)
