如何在将列表插入sqlite db

时间:2019-07-25 06:51:18

标签: python sqlite beautifulsoup

我有一个简单的代码,该代码应为我提供来自我在亚马逊上的愿望清单的价格和说明。不幸的是,在执行过程中,我遇到了一个与我的插入语句有关的错误。

from bs4 import BeautifulSoup
import requests
import sqlite3
import movie

#Getting my wishlist html
page = requests.get("some_url").text
soup = BeautifulSoup(page,'lxml')
soupd = soup.div


#Looking for item price  and descriptions

prices =[]
for price_container in soupd.find_all('span',class_='a-price'):
    price = price_container.span.text
    prices.append(price)

descriptions=[]
for description_container in soupd.find_all('h3',class_='a-size-base'):
    description = description_container.a.text
    descriptions.append(description)



conn = sqlite3.connect('movie_prices.db')
c = conn.cursor()

print(len(descriptions))
print(len(prices))

c.executemany('INSERT INTO movies (price,title) VALUES (?, ?)', prices,descriptions)
conn.commit()
conn.close()

错误信息如下:

c.executemany('插入电影(价格,标题)值(?,?)',价格,说明) TypeError:函数正好接受2个参数(给定3个参数)

2 个答案:

答案 0 :(得分:1)

您需要压缩两个列表

c.executemany('INSERT INTO movies (price,title) VALUES (?, ?)', zip(prices,descriptions))

参数必须成对传递,而zip会给您

答案 1 :(得分:0)

documentation表明应该使用括号来传递多个参数:

c.executemany('INSERT INTO movies (price,title) VALUES (?, ?)', (prices,descriptions,))

也可以对单个查询使用execute()