Introduce the following
notebooks are great for exploring code and ideas
they provide an interactive environment with tab completion for objects in code, meaning you can explore libraries and methods in a way not possible via terminal or in some ide's
Some important things to help you in your development with jupyter
key | function |
---|---|
h | pulls out help menu |
shift + enter | runs notebook cell |
? after an object (function, method, or object of class) | shows object signature |
a | creates cell on top of current cell |
b | creates cell below current cell |
x | deletes current cell |
example
import os
os?
os.<tab>
# shows functions in "os" module
import os
os?
py_list = [1, 3, 4, 5.0]
py_list.append(40)
print(py_list)
[i for i in py_list]
[i * 3 for i in py_list]
542
# your code here
store value by key
ages = {'alex': 23, 'mike': 25}
print(ages['alex'])
1 -> bill
2 -> jane
3 -> mary
name your dictionary "students"
# your code here
The two most important methods are GET and POST check out the other methods
GET requests retrieve data from a url, you can add parameters to your query to get the page you want!
Parameters are like dictionaries!
the key is the query, the value is whatever you're requesting!
the format is:
sitename.com/whatever/path?key=value
The important thing here is noting the "?", and the "key=value"
Example:
Youtube searches for videos using the key "search_query"
requests is a python library for handling http requests
it is extremely simple to use and is very intuitive
requests.get
import requests
requests.get?
params = {'search_query': 'cats'}
url = 'https://youtube.com/results'
r = requests.get(url, params)
print(r.url)
# html of the page (first 500 characters)
print(r.content[:500])
site = 'https://twitter.com'
# your code here
html is the skeleton of websites. You can view a website's html through the developer console. to pull out the developer console in chrome:
os | key |
---|---|
linux/windows | ctrl + shift + j |
osx | Cmd + Opt + K |
BeautifulSoup is a library used to parse html and xml
important methods:
soup.find('p')
soup.find(id='myid')
soup.find(class_='myclass')
soup.find_all('myclass')
from bs4 import BeautifulSoup
# init
soup = BeautifulSoup('<div class="title"><p> Pie </p></div><div class="title"><p> xyz </p></div>',
'html5lib')
print(soup.text)
print(soup.find('p'))
print(soup.find(class_='title'))
print(soup.find_all(class_='title'))
print([i.text for i in soup.find_all(class_='title')])
url = 'https://nbconvert.readthedocs.io/en/latest/usage.html'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html5lib')
print(soup.find('h1').text)
# your code here
DataFrame from the pandas module transforms a list or dictionary into a table
In turn we can export the table to a csv object
csv stands for comma separated values, and is the common way to view data, similar to excel files
the format is
row1col1,row1col2
row2col1,row2col2
which is equivalent to the table
row1col1 | row1col2 |
---|---|
row2col1 | row2col2 |
from pandas import DataFrame
data = [1, 2, 3, 4]
df = DataFrame(data, columns=['x'])
df
path = 'test.csv'
df.to_csv(path, index=False)
%cat test.csv