Question
Background: Part 2: Scraping Business Insider for Stock Prices Now, we need to scrape the Business Insider website for the current stock prices of companies.
Background:
Part 2: Scraping Business Insider for Stock Prices
Now, we need to scrape the Business Insider website for the current stock prices of companies. This is more complex than the previous scraping task, as it will involve creating and executing a function. You have been provided with the majority of the code for the function but will need to complete it.
This function will carry out three major processes: First, it will make a request for the web page on Business Insider for the specified stock symbol. This page will contain information about the current pricing of the stock that we will collect using BeautifulSoup. The next section of the code will entail transforming the html of the page into a soup, from which we will extract our data. We'll isolate the HTML class containing the price information, then scrape the current price, absolute change in price, and relative change in price from that section. Finally, the function will return a dictionary containing all of the scraped information and the ticker so that we have a consistent data structure for use in other places of our code.
In order to complete this function, you'll need to add the code for finding and extracting the text for the four parts of the scrape, as well as adding the stored variables to the dictionary that will be returned from the function.
Open your web console
Isolate the section containing the stock pricing information by using a .find() for the price section. Store this into price_section for the following steps
Within the price_section, .find() the HTML class for the current price and store this into the variable current_price.
Within the price_section, .find() the HTML class for the absolute change and store this into the variable absolute_change.
Within the price_section, .find() the HTML class for the relative change and store this into the variable relative_change.
Add the current_price, absolute_change, and relative_change variables to the return dictionary for each of their respective keys.
Hints:
You'll need to .strip() the results on the text scrapes.
Don't forget to use your Chrome Inspector! You'll definitely want to check out the inside of the class tag price-section__row.
Remember: Isolate the section you want to get data out of, then pick the specific tags useful for extraction.
Don't forget to return values from your function!!
This is what I have programmed:
#TODO: Create a function that scrapes the business insider page and returns the price information of the requested stock def scrape_stock(stock): res = requests.get(f'https://markets.businessinsider.com/stocks') html = res.text soup = BeautifulSoup(html) row_html = soup.find_all(class_="stock") raw_data = [] for block in row_html: row = { "Symbol": stock, "Current_Price":block.find(class_="price-section_curent-price").text.strip(), "Absolut_Change":block.find(class_="price-section_absolut_change").text.strip(), "Relative_Change":block.find(class_="price-section_relative_change").text.strip() } raw_data.append(row) stock_df = pd.DataFrame(raw_data) return stock_df
# TODO: Execute `scrape_stock` with the argument "AAPL" scrape_stock('AAPL')
I should be getting
Once you've completed your function, test it by passing "AAPL" as a test case. Your executed function should return something similar to the following:
{ 'Symbol': 'AAPL', 'Current_Price': '146.97', 'Absolute_Change': '+0.14', 'Relative_Change': '+0.10%' }
But I am not...
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started