首页 > 解决方案 > 如何在将列表插入sqlite db期间修复插入错误

问题描述

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

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('INSERT INTO movies (price,title) VALUES (?, ?)', prices,descriptions) TypeError:函数只需要 2 个参数(给定 3 个)

标签: pythonsqlitebeautifulsoup

解决方案


You need to zip your two lists

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

The arguments need to be passed as pairs, which zip will give you


推荐阅读