- Python Web Scraping Cookbook
- Michael Heydt
- 294字
- 2021-06-30 18:44:08
Getting ready
First make sure you have access to a PostgreSQL data instance. Again, you can install one locally, run one in a container, or get an instance in the cloud.
As with MySQL, we need to first create a database. The process is almost identical to that of MySQL but with slightly different commands and parameters.
- From the terminal execute the psql command at the terminal. This takes you into the psql command processor:
# psql -U postgres
psql (9.6.4)
Type "help" for help.
postgres=#
- Now create the scraping database:
postgres=# create database scraping;
CREATE DATABASE
postgres=#
- Then switch to the new database:
postgres=# \connect scraping
You are now connected to database "scraping" as user "postgres".
scraping=#
- Now we can create the Planets table. We first need to create a sequence table:
scraping=# CREATE SEQUENCE public."Planets_id_seq"
scraping-# INCREMENT 1
scraping-# START 1
scraping-# MINVALUE 1
scraping-# MAXVALUE 9223372036854775807
scraping-# CACHE 1;
CREATE SEQUENCE
scraping=# ALTER SEQUENCE public."Planets_id_seq"
scraping-# OWNER TO postgres;
ALTER SEQUENCE
scraping=#
- And now we can create the table:
scraping=# CREATE TABLE public."Planets"
scraping-# (
scraping(# id integer NOT NULL DEFAULT nextval('"Planets_id_seq"'::regclass),
scraping(# name text COLLATE pg_catalog."default" NOT NULL,
scraping(# mass double precision NOT NULL,
scraping(# radius double precision NOT NULL,
scraping(# description text COLLATE pg_catalog."default" NOT NULL,
scraping(# moreinfo text COLLATE pg_catalog."default" NOT NULL,
scraping(# CONSTRAINT "Planets_pkey" PRIMARY KEY (name)
scraping(# )
scraping-# WITH (
scraping(# OIDS = FALSE
scraping(# )
</span>scraping-# TABLESPACE pg_default;
CREATE TABLE
scraping=#
scraping=# ALTER TABLE public."Planets"
scraping-# OWNER to postgres;
ALTER TABLE
scraping=# \q
To access PostgreSQL from Python we will use the psycopg2 library, so make sure it is installed in your Python environment using pip install psycopg2.
We are now ready to write Python to store the planets data in PostgreSQL.
推薦閱讀
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- 微商之道
- HTML5 Game development with ImpactJS
- 網絡的琴弦:玩轉IP看監控
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 大話社交網絡
- Spring 5.0 Projects
- 物聯網之霧:基于霧計算的智能硬件快速反應與安全控制
- Unity Artificial Intelligence Programming
- INSTANT KineticJS Starter
- SAE原理與網絡規劃
- TD-LTE無線網絡規劃與設計
- jQuery Mobile Web Development Essentials
- 計算機網絡技術及應用
- 物聯網場景設計與開發(初級)