Reputation: 21
I have a basic web page running and I have web scraped some temperatures for the week. The output on the page is displaying perfect on the web page. I now want to input these into my SQLite3 database. I've tried finding some tutorials but couldn't find any clear ones. Can someone guide me on the right path?
Thanks in advance.
def weather_():
page = requests.get("https://www.bbc.co.uk/weather/0/2643743")
soup = BeautifulSoup(page.content, 'html.parser')
today = soup.find('div',{'data-component-id' : 'forecast'})
temp = today.find(class_ = 'wr-day-temperature')
low_temp = (temp.get_text())
return low_temp
Upvotes: 1
Views: 1680
Reputation: 6543
I know that this question already has an accepted answer which will work for your purposes, but I just want to mention that you should always use query parameters rather than string formatting to create SQL statements with variables. So instead of:
curs.execute("INSERT INTO your_table_name low_temp='{}'".format(low_temp))
You should use:
curs.execute("INSERT INTO your_table_name low_temp=?", (low_temp,))
The documentation confirms that this is the way to go:
Usually your SQL operations will need to use values from Python variables. You shouldn’t assemble your query using Python’s string operations because doing so is insecure; it makes your program vulnerable to an SQL injection attack (see https://xkcd.com/327/ for humorous example of what can go wrong).
I realise that for this small example program it's unlikely to make any difference, but it's best to get into good habits.
Upvotes: 1
Reputation: 1284
If you want to just store your low_temp
in database, then do a simple SQL insert
import sqlite3
conn = sqlite3.connect('your_database.sqlite3', check_same_thread=False)
curs = conn.cursor()
curs.execute("INSERT INTO your_table_name low_temp='{}'".format(low_temp))
conn.commit()
I recommend you to read some documentation
Upvotes: 0