You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
5 | New York Magazine | 50 Issues of New York Magazine | /cities/1719-newyork-citywide/deals/594056-50-issues-of-new-york-magazine | NYC Citywide | | 30 | 15
59
+
id | title |link| location | original_price | price | end_date
2 | Thrilling Live-Action Escape Game for Six | https://www.livingsocial.com/deals/1370546-thrilling-live-action-escape-game-for-six | San Francisco, CA | 169| 99 | 2015-02-08 08:00:00
63
+
3 | $30 to Spend on Food and Drink | https://www.livingsocial.com/cities/15-san-francisco/deals/1234440-30-to-spend-on-food-and-drink | San Francisco, CA | 30 | 15 | 2015-07-29 11:59:00
64
+
4 | $50 Toward Asian-Inspired Seafood, Steaks & More | https://www.livingsocial.com/deals/1333444-50-toward-asian-inspired-seafood-steaks-more | San Francisco, CA | 50| 25 | 2015-02-19 12:59:00
65
+
5 | Spa Packages with Jacuzzi and Pool Access| https://www.livingsocial.com/deals/1278806-spa-packages-with-jacuzzi-and-pool-access | San Francisco, CA | 120| 80 | 2015-09-17 11:59:59
66
66
(5 rows)
67
67
```
68
68
69
69
Try a few of these select queries:
70
70
71
71
```psql
72
-
scrape=# select * from deals where title like ('%Yoga');
73
-
scrape=# select * from deals where description like ('%Yoga%');
74
-
scrape=# select * from deals where description like ('%Dinner');
75
-
scrape=# select link from deals where description like ('%Photography%');
76
72
scrape=# select title from deals limit 30;
73
+
scrape=# select link from deals where price < 50;
74
+
scrape=# select title from deals where end_date < '2015-02-08';
75
+
scrape=# select * from deals where title like ('%Yoga');
77
76
```
78
77
79
78
Notice how the strings we’re searching for contains `%` – this is a wildcard character. This is essentially saying “find deals where the title contains “Yoga”. Learn more about [querying Postgres](http://www.postgresql.org/docs/8.4/static/tutorial-select.html).
The `drivername` is the type of database we're using – Postgres. Since we're using Postgres that we installed on our own computer, the location, or the `host` is `localhost`. The port is the default port that Postgres listens on.
73
75
74
-
The `username` is _your_ username for your machine. The `password` may not be needed, or may be the password used when setting up Postgres initially.
76
+
The `username` is _your_ username for your machine. The `password` may not be needed (just an empty string, `'password': ''`), or may be the password used when setting up Postgres initially.
75
77
76
78
The `database` is the name of the database we created earlier, `postgres=# create database scrape;`.
77
79
@@ -108,12 +110,14 @@ Last item I want to point out before we move on is the usage of the double astri
108
110
So first, our dictionary looks like:
109
111
110
112
```python
111
-
DATABASE= {'drivername': 'postgres',
112
-
'host': 'localhost',
113
-
'port': '5432',
114
-
'username': 'lynnroot',
115
-
'password': 'root',
116
-
'database': 'scrape'}
113
+
DATABASE= {
114
+
'drivername': 'postgres',
115
+
'host': 'localhost',
116
+
'port': '5432',
117
+
'username': 'lynn',
118
+
'password': '',
119
+
'database': 'scrape'
120
+
}
117
121
```
118
122
119
123
Then, the `URL()` function will parse out the elements, and create the following URL for the `create_engine()` function to read:
Last, we define our actual table by inheriting from `DeclarativeBase` and setting up how we want to define each field we want to collect. We also have to import a few more things from SQLAlchemy:
147
151
148
152
```python
149
-
from sqlalchemy import create_engine, Column, Integer, String
153
+
from sqlalchemy import create_engine, Column, Integer, String, DateTime
150
154
151
155
# <--snip-->
152
156
@@ -156,12 +160,11 @@ class Deals(DeclarativeBase):
We give our class a table name, “deals”, as well as 8 fields. Each field will be mapped to a column in our table which it's created through `create_deals_table()`.
@@ -171,7 +174,7 @@ For each field, we define the type of field that it is, `Integer` for our primar
171
174
All together:
172
175
173
176
```python
174
-
from sqlalchemy import create_engine, Column, Integer, String
177
+
from sqlalchemy import create_engine, Column, Integer, String, DateTime
175
178
from sqlalchemy.ext.declarative import declarative_base
176
179
from sqlalchemy.engine.url importURL
177
180
@@ -200,12 +203,11 @@ class Deals(DeclarativeBase):
0 commit comments