Python Forum
Webscrape using RPi and SQlite database, always write the last value in database - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Webscrape using RPi and SQlite database, always write the last value in database (/thread-40376.html)



Webscrape using RPi and SQlite database, always write the last value in database - Armond - Jul-19-2023

Hi!
I have some code that will webscrape a value and store it in a SQlite database.
My problem is that only the last value is stored in the database.
When I do the same with windows, it stores all values in the database when webscraping.
I dont know why it only stores the last value.


webscrapeall.py
import requests
import sqlite3
import datetime 

from bs4 import BeautifulSoup
URL = "http://ludvikasegel.com/wx/cloudbase.asp"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
molnbas = soup.find_all("div", class_="cloudbase")
print (molnbas[0].text.strip())


with sqlite3.connect('database.db') as con, open("/home/admin/schema.sql") as f:
    con.executescript(f.read())


    timestamp = datetime.datetime.now().isoformat()  # timestamp, change to whatever you want
    desired_value = molnbas[0].text.strip()
    

with sqlite3.connect("/home/admin/database.db") as sqlite_connection:
    sqlite_connection.execute(
        "INSERT INTO table_name VALUES (?, ?)",
        (timestamp, desired_value)
    )
schema.sql
DROP TABLE IF EXISTS table_name;

CREATE TABLE table_name (
    timestamp TEXT,
    value INTEGER
);