diff --git a/homework1/Dockerfile b/homework1/Dockerfile new file mode 100644 index 0000000..90a5d57 --- /dev/null +++ b/homework1/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.10.2-alpine3.15 +# Create directories +RUN mkdir -p /root/workspace/src +COPY ./web_scraping_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/homework1/connect.py b/homework1/connect.py new file mode 100644 index 0000000..95b61e5 --- /dev/null +++ b/homework1/connect.py @@ -0,0 +1,26 @@ +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/homework1/docker-compose.yml b/homework1/docker-compose.yml new file mode 100644 index 0000000..9518836 --- /dev/null +++ b/homework1/docker-compose.yml @@ -0,0 +1,10 @@ +psql-db: + image: 'postgres:14' + container_name: psql-db1 + environment: + - PGPASSWORD=123456 + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=123456 + ports: + - '5434:5432' + diff --git a/homework1/dockerfile b/homework1/dockerfile new file mode 100644 index 0000000..fabeacf --- /dev/null +++ b/homework1/dockerfile @@ -0,0 +1,10 @@ +FROM python:3.10.2-alpine3.15 +# Create directories +RUN mkdir -p /root/workspace/src +COPY ./web_scraping_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/homework1/output2.csv b/homework1/output2.csv new file mode 100644 index 0000000..fc6382c --- /dev/null +++ b/homework1/output2.csv @@ -0,0 +1,43 @@ +column1,column2,column3 +count,count1," +Python 3.11.3, Python 3.10.11 and 3.12.0 alpha 7 are available +" +count,count2,Python 3.12.0 alpha 7 +count,count3,Python 3.11.3 +count,count4,Python 3.10.11 +count,count5,We hope you enjoy the new releases! +count,count6," +Python 3.12.0 alpha 6 released +" +count,count7," +Python 3.11.2, Python 3.10.10 and 3.12.0 alpha 5 are available +" +count,count8,Python 3.12.0 alpha 5 +count,count9,Python 3.11.2 +count,count10,Python 3.10.10 +count,count11,"Thanks to all of the many volunteers who help make Python Development and these releases possible! Please consider supporting our efforts by volunteering yourself or through organization contributions to the Python Software Foundation.https://www.python.org/psf/ Your friendly release team,Ned Deily @nadSteve Dower @steve.dowerPablo Galindo Salgado @pablogsalŁukasz Langa @ambvThomas Wouters @thomas" +count,count12," +Python 3.12.0 alpha 4 released +" +count,count13," + Python 3.11.1, 3.10.9, 3.9.16, 3.8.16, 3.7.16, and 3.12.0 alpha 3 are now available +" +count,count14," +Python 3.12.0 alpha 3" +count,count15," +Python 3.11.1" +count,count16," +Python 3.10.9" +count,count17," +Python 3.9.16" +count,count18," +Python 3.8.16" +count,count19," +Python 3.7.16" +count,count20," +Python 3.12.0 alpha 2 released +" +count,count21," +Python 3.12.0 alpha 1 released +" +count,count22,And now for something completely different diff --git a/homework1/web_scraping_sample.py b/homework1/web_scraping_sample.py new file mode 100644 index 0000000..7616889 --- /dev/null +++ b/homework1/web_scraping_sample.py @@ -0,0 +1,22 @@ +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("h3") +titles_list=[] +count=1 +for title in titles: + d={} + d['column1']=f'count' + d['column2']=f'count{count}' + d['column3']=title.text + count+=1 + titles_list.append(d) + filename='output2.csv' +with open(filename,'w',newline='') as f: + writer = csv.DictWriter(f,['column1','column2', 'column3']) + writer.writeheader() + writer.writerows(titles_list) +f.close() +