I'll help you write code to run the FNSPID data scraper in Google Colab. Based on the repository context, I'll create a step-by-step implementation:
# Install required packages
!pip install -r requirements.txt
# Clone the repository
!git clone https://github.com/Zdong104/FNSPID.git
%cd FNSPID/data_scraper
# Create necessary directories
!mkdir -p headline_scraper/headlines
!mkdir -p headline_scraper/lists
!mkdir -p news_content_scraper/news_contents
!mkdir -p news_content_scraper/lists
!mkdir -p stock_price_scraper/yahoo
!mkdir -p stock_price_scraper/lists
# Install Chrome and ChromeDriver for Selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
# Create a sample stock list file (list_a.csv) in the lists folder
import pandas as pd
# Create sample data for stocks starting with 'a'
sample_stocks = pd.DataFrame({
'Stock_name': ['aapl', 'amd', 'amzn'],
'Mark': [0, 0, 0],
'Desired_page': [0, 0, 0]
})
# Save to all scraper list directories
sample_stocks.to_csv('headline_scraper/lists/list_a.csv', index=False)
sample_stocks.to_csv('news_content_scraper/lists/list_a.csv', index=False)
sample_stocks.to_csv('stock_price_scraper/lists/list_a.csv', index=False)
# Function to run headline scraper
def run_headline_scraper():
%cd headline_scraper
!python "find_headlines v0.2.py"
%cd ..
# Function to run news content scraper
def run_news_content_scraper():
%cd news_content_scraper
!python "find_content v0.2.py"
%cd ..
# Function to run stock price scraper
def run_stock_price_scraper():
%cd stock_price_scraper
!python get_price_from_yahoo.py
%cd ..
# Main execution
print("Starting data scraping process...")
# 1. Run headline scraper
print("\nRunning headline scraper...")
run_headline_scraper()
# 2. Run news content scraper
print("\nRunning news content scraper...")
run_news_content_scraper()
# 3. Run stock price scraper
print("\nRunning stock price scraper...")
run_stock_price_scraper()
print("\nScraping process completed!")
To use this code in Google Colab:
- Create a new Colab notebook
- Copy and paste this code into a cell
- Run the cell
Important notes:
-
The code assumes you want to scrape stocks starting with 'a' (aapl, amd, amzn). You can modify the sample_stocks DataFrame to include different stocks.
-
When the scrapers run, they will prompt for input. Enter 'a' when prompted to scrape stocks from list_a.csv.
-
The scraping process may take some time depending on the number of stocks and amount of data.
-
You may need to handle Google Colab timeouts for long-running scrapes by either:
- Using smaller batches of stocks
- Enabling "Runtime -> Change runtime type -> High-RAM" in Colab
- Using Colab Pro for longer runtimes
-
The scraped data will be saved in the respective directories:
- Headlines:
headline_scraper/headlines/
- News content:
news_content_scraper/news_contents/
- Stock prices:
stock_price_scraper/yahoo/
Remember that web scraping should be done responsibly and in accordance with the websites' terms of service and rate limits.
Would you like me to explain any part of the code in more detail?