Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vivekanada_session_4VP20CS032_JNAPAK_M #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions docker1hw/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
RUN pip3 install psycopg2-binary --user
26 changes: 26 additions & 0 deletions docker1hw/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "3"
services:
pyhton_service:
build:
context: ./
dockerfile: Dockerfile
image: workshop1
container_name: workshop_python_container
stdin_open: true # docker attach container_id
tty: true
ports:
- "8000:8000"
volumes:
- .:/app
depends_on:
- postgres_service

postgres_service:
image: postgres
container_name: workshop_postgres_container
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: admin
volumes:
- .:/var/lib/postgres
35 changes: 35 additions & 0 deletions docker1hw/web_scraping_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import requests
from bs4 import BeautifulSoup
import psycopg2

#edited the script
url = 'https://blog.python.org/'
response = requests.get(url)


soup = BeautifulSoup(response.content, 'html.parser')


titles = soup.find_all('h3', class_='post-title')
dates = soup.find_all('h2', class_='date-header')


conn = psycopg2.connect(database="mydatabase", user="postgres", password="admin", host="localhost", port="5432")


cur = conn.cursor()


cur.execute('CREATE TABLE IF NOT EXISTS blog (id SERIAL PRIMARY KEY, title TEXT, date DATE)')


for i in range(len(titles)):
title = titles[i].get_text()
date = dates[i].get_text()
cur.execute('INSERT INTO blog (title, date) VALUES (%s, %s)', (title, date))


conn.commit()
cur.close()
conn.close()