如何使用SQLite3在页面加载/刷新时将数据插入SQLite?

时间:2019-02-16 00:00:43

标签: python sqlite beautifulsoup

我已经将一些天气信息抓到了我的网站上,现在想在每次我的网站上的天气页面刷新时将它们放入sqlite3。我遇到的问题是由于天气已经显示在我的网站上,因此将信息插入数据库中。

def weather_():
    page = requests.get("https://www.bbc.com/weather/0/2562305")
    soup = BeautifulSoup(page.content, 'html.parser') 
    today = soup.find('div',{'data-component-id' : 'forecast'})
    temp = today.find(class_ = 'wr-day-temperature__low')
    low_temp = (temp.get_text())
    return low_temp

2 个答案:

答案 0 :(得分:1)

存储天气数据

以下是有关sqlite的有用教程:http://www.sqlitetutorial.net/sqlite-python/insert/

和另一个S.O.链接:Python and SQLite: insert into table

而且非常有用:https://docs.python.org/2/library/sqlite3.html


import sqlite3
conn = sqlite3.connect('weather.db')
c = conn.cursor()

# Create table
c.execute('''CREATE TABLE weather
             (lowtemp text)''')

# Insert a row of data
c.execute("INSERT INTO weather VALUES (low_temp)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

使用API​​

调查您的抓取内容,英国广播公司仅使用Meteo(https://www.meteogroup.com/weather-api)。

此Met Office https://www.metoffice.gov.uk/public/weather对于获取原始数据并跳过屏幕抓取可能非常有用。

数据点:https://www.metoffice.gov.uk/datapoint/about

以下是历史数据:https://www.metoffice.gov.uk/pub/data/weather/uk/climate/stationdata/newtonriggdata.txt

答案 1 :(得分:0)

您需要创建一个数据库文件和一个表格来存储您的天气信息。

有关sqlite3的安装,请参考to this doc

  

创建表天气(ID整数自动增加,低温   VARCHAR(25))

添加适当的功能以更新数据库。

def create_connection(db_file):
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)
    return None

def weather_():
    page = requests.get("https://www.bbc.com/weather/0/2562305")
    soup = BeautifulSoup(page.content, 'html.parser') 
    today = soup.find('div',{'data-component-id' : 'forecast'})
    temp = today.find(class_ = 'wr-day-temperature__low')
    low_temp = (temp.get_text())
    return low_temp

//Getting the data
low_temp = weather_()
//Updating the database
your_db_file= '/path_to_your_db_file/your_wheather_db.sqlite'
connector = create_connection(your_db_file)
cursor = connector .cursor()
sql = "INSERT INTO Weather(low_temperature) VALUES ('"+str(low_temp )+"')"
cursor.execute(sql)
conn.commit()
conn.close()