From b7f131878db9f19fea1862931b691d8610703900 Mon Sep 17 00:00:00 2001 From: Niriksha-2002 <116286277+Niriksha-2002@users.noreply.github.com> Date: Tue, 25 Apr 2023 00:41:06 +0530 Subject: [PATCH] Add files via upload --- homework/Dockerfile | 10 ++++++++++ homework/connect.py | 25 +++++++++++++++++++++++++ homework/docker-compose.yml | 9 +++++++++ homework/output2.csv | 12 ++++++++++++ homework/web_scrapping_sample.py | 12 ++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 homework/Dockerfile create mode 100644 homework/connect.py create mode 100644 homework/docker-compose.yml create mode 100644 homework/output2.csv create mode 100644 homework/web_scrapping_sample.py diff --git a/homework/Dockerfile b/homework/Dockerfile new file mode 100644 index 0000000..1ce5652 --- /dev/null +++ b/homework/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.10.2-alpine3.15 +# Create directories +RUN mkdir -p /root/workspace/src +COPY ./web_scrapping_sample.py /root/workspace/src +# Switch to project directory +WORKDIR /root/workspace/src +# Install required packages +RUN pip install --upgrade pip +RUN pip install requests bs4 html5lib + diff --git a/homework/connect.py b/homework/connect.py new file mode 100644 index 0000000..7ab776b --- /dev/null +++ b/homework/connect.py @@ -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() diff --git a/homework/docker-compose.yml b/homework/docker-compose.yml new file mode 100644 index 0000000..15f0a8d --- /dev/null +++ b/homework/docker-compose.yml @@ -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' diff --git a/homework/output2.csv b/homework/output2.csv new file mode 100644 index 0000000..c37ecce --- /dev/null +++ b/homework/output2.csv @@ -0,0 +1,12 @@ +mport 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() diff --git a/homework/web_scrapping_sample.py b/homework/web_scrapping_sample.py new file mode 100644 index 0000000..06b1147 --- /dev/null +++ b/homework/web_scrapping_sample.py @@ -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()