gpt4 book ai didi

python - 如何将 Beautiful Soup 输出保存到我​​的 SQLite 数据库中?

转载 作者:行者123 更新时间:2023-12-01 09:29:15 25 4
gpt4 key购买 nike

我正在运行一个基本网页,并且我已经在网络上抓取了本周的一些温度。页面上的输出在网页上显示完美。我现在想将这些输入到我的 SQLite3 数据库中。我尝试寻找一些教程,但找不到任何明确的教程。有人可以引导我走上正确的道路吗?

提前致谢。

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

最佳答案

我知道这个问题已经有了一个可以满足您的目的的可接受的答案,但我只想提一下,您应该始终使用查询参数而不是字符串格式来创建带有变量的 SQL 语句。所以代替:

curs.execute("INSERT INTO your_table_name low_temp='{}'".format(low_temp))

您应该使用:

curs.execute("INSERT INTO your_table_name low_temp=?", (low_temp,))

documentation确认这是要走的路:

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).

我意识到对于这个小示例程序来说它不太可能产生任何影响,但最好养成良好的习惯。

关于python - 如何将 Beautiful Soup 输出保存到我​​的 SQLite 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com