Practical Data Science
Data collection and scraping
The first step in data science …
… is to get some data
You will typically get data in one of four ways:
1. Directly download a data file (or files) manually
2. Query data from a database
3. Query an API (usually web-based, these days)
4. Scrap data from a webpage
Issuing HTTP queries
As a data scientist, you’ll be dealing with hundreds of different libraries and APIs, and trying to commit them all to memory is not useful. Instead, what you need to develop is the ability to quickly find a library and function call that you need to accomplish some task. For example, even if you know nothing about the in this case, you want to download the content of some URL. You can type into Google something like “Python download url content” (I just picked this precise phrasing randomly, feel free to try some variants on this). The first result for my search is a Stack Overflow page: How do I download a file over HTTP using Python?.
This code issues an “HTTP GET” request to load the content of the paper, and returns it in the response object. The status_code field contains the “200” code, which indicates a successful query, and the headers field contains meta-information about the page (in this case, you could see, for instance, that despite the URL, we’re actually hosting this page on github). If you want to see the actual content of the page, you can use the response.content or response.text
we can try out this code:
import requests
response = requests.get(“http://www.cmu.edu")
print(“Status Code:”, response.status_code)
print(“Headers:”, response.headers) # response.content, response.text
print(“Headers:”, response.headers) # response.content, response.text
Collecting data from web-based sources
With those general caveats in mind, let’s dive a bit more deeply into the specific case of gather data from an web-based source, which is one of the more common forms of querying data. It will also serve as an introduction to the type of Python coding that you’ll do.
The first step of collecting web-based data is to issue a request for this data via some protocol: HTTP (HyperText Transfer Protocol) or HTTPS (the secure version).
Data formats
The three most common formats (judging by my completely subjective experience):
1. CSV (comma separate value) files
2. JSON (Javascript object notation) files and strings
3. HTML/XML (hypertext markup language / extensible markup language) files and strings
Regular expressions
Once you have loaded data (or if you need to build a parser to load some other data format), you will often need to search for specific elements within the data
Matching multiple potential characters
The real power of regular expressions comes in the ability to match multiple possible sequence of characters
Special characters in regular expressions: .^$*+?{}\[]|() (if you want to match these characters exactly, you need to escape them: \$)
Get GritinAI’s stories in your inbox
Join Medium for free to get updates from this writer.
Match sets of characters:
- Match the character ‘a’: a
- Match the character ‘a’, ‘b’, or ‘c’: [abc]
- Many any character except ‘a’, ‘b’, or ‘c’: [^abc]
- Match any digit: \d (= [0–9])
- Match any alpha-numeric: \w (= [a-zA-z0–9_])
- Match whitespace: \s (= [ \t\n\r\f\v])
- Match any character: . (including newline with re.DOTALL)
Matching repeated characters
Can match one or more instances of a character (or set of characters)
Some common modifiers:
• Match character ‘a’ exactly once: a
• Match character ‘a’ zero or one time: a?
• Match character ‘a’ zero or more times: a*
• Match character ‘a’ one or more times: a+
• Match character ‘a’ exactly n times: a{n} Can combine these with multiple character matching:
• Match all instances of “<something> science” where <something> is an alphanumeric string with at least one character
- \w+\s+science
Grouping
We often want to obtain more information that just whether we found a match or not (for instance, we may want to know what text matched)
Grouping: enclose portions of the regular expression in parentheses to “remember” these portions of the match
Substitutions
Regular expressions provide a power mechanism for replacing some text with other text.
To include text that was remembered in the matching using groups, use the escaped sequences \1, \2, … in the substitution text
Ordering and greedy matching
There is an order of operations in regular expressions
abc|def matches the strings “abc” or “def”, not “ab(c or d)ef”
You can get around this using parentheses e.g. a(bc|de)f
This also creates a group, use a(?:bc|de)f if you don’t want to capture it
By default, regular expressions try to capture as much text as possible (greedy matching)
<(.*)> applied to <a>text</a> will match the entire expression
If you want to capture the least amount of text possible use <(.*?)> this will just match the <a> term
link to the notebook for this is found in this link
Check out more updates in our website www.gritinai.com and our handles.








