Я пытаюсь написать скребок BeautifulSoup для захвата изображений непосредственно с URL-адресов и загрузки их в таблицу postgres. Когда я запускаю приведенный ниже код в терминале, приглашение зависает, как будто оно что-то делает, но в таблице нет данных. (Я знаю, что URL-адреса верны.)
import requests
import pandas as pd
import psycopg2
import datetime
import time
from bs4 import BeautifulSoup
# INSERT THE DATA
def create_table():
'''Create table in database.'''
try:
conn = psycopg2.connect('''
conn string
''')
cur = conn.cursor()
# Create table for database.
cur.execute('''CREATE TABLE if not exists tablename (
timestamp TIMESTAMP NOT NULL
,pic_col_1 bytea
,pic_col_2 bytea
,pic_col_3 bytea
)''')
except Exception as e:
print(e)
return
conn.commit()
conn.close()
create_table()
def insert_data(timestamp, s1, s2, s3):
'''Insert data into the database.'''
conn = psycopg2.connect('''
conn string
''')
cur = conn.cursor()
cur.execute('''INSERT INTO tablename
(timestamp, img1, img2, img3)
VALUES (%s, %s, %s, %s)''', (timestamp, s1, s2, s3))
conn.commit()
conn.close()
# Save data in a Postgresql database.
while True:
# GET THE DATA
img1 = requests.get('http://website.com/files/apple.jpg')
img2 = requests.get('http://website.com/files/banana.jpg')
img3 = requests.get('http://website.com/files/cranberry.jpg')
ximg1 = img1.extract()
ximg2 = img2.extract()
ximg3 = img3.extract()
time_now = datetime.datetime.today()
timestamp = datetime.datetime.strftime(time_now, "%Y-%m-%d %H:%M:%S")
print(timestamp)
insert_data(timestamp, ximg1, ximg2, ximg3)