我从亚马逊抓取一些数据,然后将这些数据插入到4个列表中。但是,当我尝试将这些列表插入数据库时,我只是得到一个类型错误:
TypeError: not all arguments converted during string formatting
但所有数据都是字符串格式。我尝试使用元组,但它无法正常工作。
# Importing Requests and BeautifulSoup Module
import requests
from bs4 import BeautifulSoup
import pymysql
# Setting Base Url
base_url = "https://www.amazon.com/s/ref=lp_6503737011_pg_2?rh=n%3A16310101%2Cn%3A%2116310211%2Cn%3A2983386011%2Cn%3A6503737011&page="
# Setting range for pagination
pagination = list(range(1,3))
# Declaring Empty Data
name = []
retailer = []
price = []
image_link = []
# Looping through pagination
for num in pagination:
url = base_url + str(num)
# Connection Error Handler
try:
r = requests.get(url)
except requests.exceptions.ConnectionError:
r.status_code = "Connection refused"
print("Connection Refused by the server")
# Setting BeautifulSoup Object
soup = BeautifulSoup(r.content, "html.parser")
# Setting Div Class of Info
g_data = soup.find_all("div", {"class": "s-item-container"})
# Getting Every Data from Info Div
for item in g_data:
imgs = soup.findAll("img", {"class":"s-access-image"})
for img in imgs:
image_link.append(img['src'])
name.append(item.contents[2].find_all('h2', {'class':'s-access-title'})[0].text)
retailer.append(item.contents[2].find_all('span', {'class':'a-size-small'})[1].text)
whole_number = str(item.contents[3].find_all('span', {'class':'sx-price-whole'})[0].text)
fractional_number = str(item.contents[3].find_all('sup', {'class':'sx-price-fractional'})[0].text)
price_1 = whole_number+"."+fractional_number
price.append(price_1)
这是抓取数据的代码。一切都很好。但是当我尝试将数据插入数据库时遇到问题。
import pymysql
db = pymysql.connect('localhost','root','','scrape')
cursor = db.cursor()
sql = """INSERT INTO wine(
NAME,RETAILER,PRICE,IMAGE_LINK) VALUES"
"""
cursor.executemany(sql, (name,retailer,price,image_link))
运行此代码时出现此错误:
TypeError Traceback (most recent call last)
<ipython-input-7-0fca81edd73c> in <module>()
6 NAME,RETAILER,PRICE,IMAGE_LINK) VALUES"
7 """
----> 8 cursor.executemany(sql, (name,retailer,price,image_link))
C:\Anaconda3\lib\site-packages\pymysql\cursors.py in executemany(self, query, args)
193 self._get_db().encoding)
194
--> 195 self.rowcount = sum(self.execute(query, arg) for arg in args)
196 return self.rowcount
197
C:\Anaconda3\lib\site-packages\pymysql\cursors.py in <genexpr>(.0)
193 self._get_db().encoding)
194
--> 195 self.rowcount = sum(self.execute(query, arg) for arg in args)
196 return self.rowcount
197
C:\Anaconda3\lib\site-packages\pymysql\cursors.py in execute(self, query, args)
162 pass
163
--> 164 query = self.mogrify(query, args)
165
166 result = self._query(query)
C:\Anaconda3\lib\site-packages\pymysql\cursors.py in mogrify(self, query, args)
141
142 if args is not None:
--> 143 query = query % self._escape_args(args, conn)
144
145 return query
TypeError: not all arguments converted during string formatting
我无法找到解决此问题的任何解决方案。请帮忙。
答案 0 :(得分:0)
%s
.executemany()
将容器容器作为第二个参数;通常这是一个元组列表更改为:
sql = """INSERT INTO wine(NAME,RETAILER,PRICE,IMAGE_LINK) VALUES (%s,%s,%s,%s);"""
to_insert = [(a,b,c,d) for a,b,c,d in zip(name,retailer,price,image_link)]
cursor.executemany(sql,to_insert)