英文:
Webscrape value from my webpage and store in database SQlite
问题
我想从我的网页中进行网页抓取,然后每隔5分钟将其发送到一个SQLite数据库
我已经完成了Python的网页抓取:
```python
import requests
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())
现在我困在如何将其存储在SQLite数据库中。
在我的数据库中,我想要该值以及一些时间戳。
有任何关于如何做或者任何好的简单教程的建议吗?
谢谢
我已经查看了一些教程,但仍然不确定如何做。
<details>
<summary>英文:</summary>
I want to webscrape a value from my webpage and send it to a SQlite database every 5 minutes
I have done the webscrape python:
import requests
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())
Now I'm stucked how to store it in a SQlite database.
In my database I want the value and a some timestamp.
Any suggestions how to do it or any good simple tutorials?
Thanx
I have checked some tutorials but am still unsure how to do it.
</details>
# 答案1
**得分**: 1
首先创建一个名为 `schema.sql` 的文件。理论上,这可以直接放入 `init_db.py` 中,但我认为最好将其分离以提高代码的清晰度。
```sql
DROP TABLE IF EXISTS table_name;
CREATE TABLE table_name (
value INTEGER,
timestamp TEXT
);
然后创建一个名为 init_db.py
的文件。
import sqlite3
with sqlite3.connect('database.db') as con, open("schema.sql") as f:
con.executescript(f.read())
将以下内容添加到每隔5分钟运行的脚本中。
import datetime
desired_value = molnbas[0].text.strip()
timestamp = datetime.datetime.utcnow().isoformat() # UTC时间戳,根据需要更改
with sqlite3.connect("database.db") as sqlite_connection:
sqlite_connection.execute(
"INSERT INTO table_name VALUES (?, ?)",
(desired_value, timestamp)
)
英文:
First make a schema.sql
file. This can in theory be put directly into init_db.py
but it's (in my opinion) good practise to separate it out for clarity.
DROP TABLE IF EXISTS table_name;
CREATE TABLE table_name (
value INTEGER,
timestamp TEXT
);
Then make an init_db.py
file
import sqlite3
with sqlite3.connect('database.db') as con, open("schema.sql") as f:
con.executescript(f.read())
Add this to your script that is run every 5th minute.
import datetime
desired_value = molnbas[0].text.strip()
timestamp = datetime.datetime.utcnow().isoformat() # UTC timestamp, change to whatever you want
with sqlite3.connect("database.db") as sqlite_connection:
sqlite_connection.execute(
"INSERT INTO table_name VALUES (?, ?)",
(desired_value, timestamp)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论