在循环中向MySQL添加多个值

2024-09-28 17:04:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我从站点中抓取一个元素列表,并从中提取两个值。texthref。我不知道如何将它们添加到循环中的mysqlDB中。我在循环中尝试了execute,没有使用它们所对应的列表,但失败了。因此,我尝试了executemany,但我认为我的格式可能不正确。我看到了一些例子,其中元组列表被馈送到executemany,我不知道在这种情况下如何做

                for name in name_eles:
                    names_list.append(name.text)
                    n_li = name.get_attribute('href')
                    names_links.append(n_li)
                sql = "INSERT INTO profiles(company, coprofile) VALUES (%s,%s)"
                val = [(name for name in name_list),(n_li for n_li in names_links)]
                cursor.executemany(sql,val)

这就是我得到的错误

MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting

Tags: textnamein列表forsqlnames站点