Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
FROM python:3.10.2-alpine3.15
COPY . .
# Install Postgres
RUN apk update
RUN apk add postgresql
RUN chown postgres:postgres /run/postgresql/
# Install requirements
COPY ./requirements.txt /tmp
RUN pip install -r /tmp/requirements.txt
# For psycopg2
RUN apk add --virtual postgresql-deps libpq-dev
# Create directories
# Create directories
RUN mkdir -p /root/workspace/src
# Mount your local file
COPY ./web_scraping_sample.py /root/workspace/src
COPY ./web_scrapping_sample.py /root/workspace/src
# Switch to project directory
WORKDIR /root/workspace/src
WORKDIR /root/workspace/src
# Install required packages
RUN pip install --upgrade pip
RUN pip install requests bs4 html5lib
25 changes: 25 additions & 0 deletions connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import psycopg2
import csv
conn = psycopg2.connect(
host="172.17.0.2",
port="5432",
dbname="demodb",
user="postgres",
password="123456"
)

cur = conn.cursor()

with open('output2.csv','r') as f:
csv_reader = csv.reader(f)
next(csv_reader)
for row in csv_reader:
if row[1] =="":
row[1] = "N/A"

cur.execute("INSERT INTO demotable (column1, column2, column3) VALUES (%s, %s, %s)",(row[0], row[1],row[2]))

conn.commit()

cur.close()
conn.close()
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
psql-db:
image: 'postgres:14'
container_name: psql-db1
environment:
- PGPASSWORD=123456
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=123456
ports:
- '5434:5432'
10 changes: 10 additions & 0 deletions output2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
column 1,column 2,column 3
"Major new features of the 3.12 series, compared to 3.11",And now for something completely different,Enjoy the new releases
More resources,Enjoy the new releases,"Major new features of the 3.12 series, compared to 3.11"
<br/>,"Major new features of the 3.12 series, compared to 3.11",More resources
,More resources,<br/>
,<br/>,And now for something completely different
,And now for something completely different,<br/>
,<br/>,Enjoy the new releases
,,"<span style=""font-size: medium;"">Major new features of the 3.12 series, compared to 3.11</span>"
,,"<span style=""font-size: medium;"">More resources</span>"
12 changes: 12 additions & 0 deletions web_scrapping_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import requests
import csv
from bs4 import BeautifulSoup
res=requests.get("https://blog.python.org/")
soup = BeautifulSoup(res.content, "html.parser")
titles = soup.find_all("h1")
with open('output2.csv','w',newline='') as f:
writer = csv.writer(f)
writer.writerow(['column 1','column 2','column 3'])
for row in titles:
writer.writerow(row)
f.close()