File Manipulation with Python

File Manipulation, Python
Python is great for automating file creation, deletion, and other types of file manipulations.  Two of the primary packages used to perform these types of tasks are os and shutil.  We'll be covering a few useful highlights from each of these. [code lang="python"] import os import shutil [/code] Batch Folder Creation If you want to create a handful of folders / directories, it's not difficult to manually do so.  But creating a few dozen folders manually gets mundane really fast. The os package contains a method, os.mkdir, that we can use in our situation. One line of code you might (though not required) want to use before you start is to change your working directory to where you want to create your list of folders: [code lang="python"] os.chdir('C:/Users/USERNAME/Documents') [/code] One problem I've…
Read More

Scraping Articles About Stocks

Python, Web Scraping
The following article will show you an example of how to scrape articles about stocks from the Web.  Specifically, we'll be looking at articles linked from http://www.nasdaq.com.  We'll be coding in Python 3. If you're not familiar with list comprehensions, you may want to check this, as we'll be using them in our code. Initial, Specific Example Let's start with a specific stock -- say, Netflix, for example.  Articles linked to a specific stock ticker from Nasdaq's website have the following pattern: http://www.nasdaq.com/symbol/TICKER/news-headlines, where TICKER is replaced with whatever ticker you want.  In our case, we will start by dealing specifically with Netflix's (NFLX) stock.  So our site of interest is: http://www.nasdaq.com/symbol/nflx/news-headlines The first step is to load the requests and BeautifulSoup packages.  Here, we'll also set the variable site equal to the…
Read More