1
我從亞馬遜刮取一些數據,然後將這些數據插入到4列表中。但是當我試圖將這些列表插入數據庫時,我只是得到一個類型錯誤:當我嘗試向MySQL插入數據時出現此錯誤 - >「TypeError:並非在字符串格式化過程中轉換的所有參數」
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
我無法找到任何解決方案來解決這個問題。請幫忙。
我試過,但得到這個無錯誤> InternalError該:(1241, '操作數應包含1列(S)') –
@RageebArtho :請參閱編輯答案。 – bernie
謝謝!它只是解決了我的問題!非常感謝你的幫助。我真的很感激。 。 –