To run the data scraper in Google Colab, you need to set up the environment, install the necessary dependencies, and run the scraper scripts. Below is a step-by-step guide with the Python code to run the data scraper in Google Colab:
Step 1: Set Up Google Colab Environment
- Open Google Colab: Google Colab
- Create a new notebook.
Step 2: Install Dependencies
First, install the required dependencies. Run the following code in a Colab cell:
# Install required dependencies
!pip install selenium pandas yfinance chardet openai matplotlib scikit-learn keras tensorflow
Step 3: Install ChromeDriver
Selenium requires a browser driver to work. In Google Colab, you can install ChromeDriver as follows:
# Install ChromeDriver
!apt-get update
!apt-get install -y wget unzip
!wget -N https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip
!unzip chromedriver_linux64.zip
!chmod +x chromedriver
!mv -f chromedriver /usr/local/bin/chromedriver
Step 4: Set Up Selenium WebDriver
Now, set up the Selenium WebDriver to use the installed ChromeDriver:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument('--headless') # Run in headless mode
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# Set up the WebDriver
service = Service('/usr/local/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=chrome_options)
Step 5: Run the Data Scraper
Now, you can run the data scraper. Below is an example of how to run the headline_scraper:
import os
import pandas as pd
# Clone the repository
!git clone https://github.com/Zdong104/FNSPID_Financial_News_Dataset.git
# Navigate to the headline_scraper directory
os.chdir('/content/FNSPID_Financial_News_Dataset/data_scraper/headline_scraper')
# Run the headline scraper
!python find_headlines.py
Step 6: Run the News Content Scraper
After scraping the headlines, you can proceed to scrape the news content:
# Navigate to the news_content_scraper directory
os.chdir('/content/FNSPID_Financial_News_Dataset/data_scraper/news_content_scraper')
# Run the news content scraper
!python find_content.py
Step 7: Run the Stock Price Scraper
Finally, scrape the stock prices:
# Navigate to the stock_price_scraper directory
os.chdir('/content/FNSPID_Financial_News_Dataset/data_scraper/stock_price_scraper')
# Run the stock price scraper
!python get_price_from_yahoo.py
Step 8: Verify the Scraped Data
You can verify the scraped data by checking the output files in the respective directories:
# Check the headline data
headline_data = pd.read_csv('/content/FNSPID_Financial_News_Dataset/data_scraper/headline_scraper/headlines/aapl.csv')
print(headline_data.head())
# Check the news content data
news_content_data = pd.read_csv('/content/FNSPID_Financial_News_Dataset/data_scraper/news_content_scraper/news_contents/aapl.csv')
print(news_content_data.head())
# Check the stock price data
stock_price_data = pd.read_csv('/content/FNSPID_Financial_News_Dataset/data_scraper/stock_price_scraper/yahoo/aapl.csv')
print(stock_price_data.head())
Step 9: Clean Up
After running the scrapers, you can clean up the environment:
# Close the WebDriver
driver.quit()
# Remove the cloned repository
!rm -rf /content/FNSPID_Financial_News_Dataset
Summary
This code sets up the environment, installs the necessary dependencies, and runs the data scraper in Google Colab. You can modify the paths and scripts as needed to scrape different data or from different sources. Make sure to follow the instructions in the repository for any additional setup or configuration.