python - 如何在将列表插入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 个)
解决方案
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
推荐阅读
- git - 覆盖 git 存储库而不丢失提交
- entity-framework - 包含路径表达式必须引用在 EF 使用关系表类型上定义的导航属性
- python - 使用 sublime 运行我的代码时出现异常:(null): can't open file '$file_basename': [Errno 2] No such file or directory
- vim - 每当启动 ViM 时运行 bash 命令
- android - FileNotFoundException:/acct/uid_99315/pid_1778/cgroup.procs 在小米 MIUI GLobal 10.1 中访问本机库时
- itfoxtec-identity-saml2 - 依赖方应该安装哪个证书?
- visual-studio-code - Visual Studio代码中物联网边缘模块图标的含义
- sql - 用 $any 续集 $not
- android - 如何从数组中选择第二个或第三个元素?
- google-analytics - Google Analytics API 的所有时间总观看次数?