Jul-19-2023, 09:11 PM
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
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.sqlDROP TABLE IF EXISTS table_name;
CREATE TABLE table_name (
timestamp TEXT,
value INTEGER
);
