我正在使用BeautifulSoup从网站检索一些数据。然后我使用PyMySQL将数据存储在MySQL中。但是,我收到以下错误: TypeError:execute()需要2到3个位置参数,但给出了7个
此错误指向“ cur.execute”命令。
这是我的代码:
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock',
user='root', passwd='---', db='mysql',
charset='utf8')
cur = conn.cursor()
cur.execute('USE HuntsPointsBusinesses')
def store (name, rating, phone, address, link):
#The error is pointing to the following line:
cur.execute('INSERT INTO HuntsPointYelp (name, rating, phone, address,
link) VALUES ("%s", "%s","%s", "%s","%s")',
name, rating, phone, address, link)
cur.connection.commit()
def getInformation(bs):
listLinks = []
restGrid = bs.find_all ("ul", {"class": "lemon--ul__373c0__1_cxs
undefined list__373c0__2G8oH"})
for i in restGrid:
link = i.find_all("a", {"href": re.compile ("/biz/.*")})
for l in link:
if "target" in l.attrs and "name" in l.attrs and "rel" in
l.attrs:
listLinks.append (l.attrs["href"])
linksList = []
for link in listLinks[0::3]:
newLink = ("https://www.yelp.com"+str(link))
linksList.append (newLink)
addressDiv = bs.find_all("div", {"class": re.compile
(".*container__373c0__19wDx u-padding-l2.*text-align--
right__373c0__1cJDF“)})
addressTag = [a.find("span", {"class": re.compile("lemon--
span__373c0__3997G")}) for a in addressDiv]
addressList = [addr.text for addr in addressTag]
phoneDiv = bs.find_all("div", {"class": re.compile
(".*container__373c0__19wDx u-padding-l2.*text-align--
right__373c0__1cJDF")})
phones_ = [p.find("p") for p in phoneDiv]
phonesList = [ph.text for ph in phones_]
ratingDiv = bs.find_all ("div", {"class": re.compile ("lemon--
div.*hidden__373c0__8Jq2I")})
ratingsList = []
for r in ratingDiv:
if "aria-label" in r.attrs:
if r.attrs is not None:
ratingsList.append (r["aria-label"])
restGrid = bs.find_all ("ul", {"class": "lemon--ul__373c0__1_cxs
undefined list__373c0__2G8oH"})
namesList = []
for i in restGrid:
h3 = i.find_all ("h3")
for h in h3:
target = h.find_all ("a")
for t in target:
if "name" in t.attrs:
if t.attrs is not None:
namesList.append (t["name"])
store (namesList, ratingsList, phonesList, addressList, linksList)
html_page = requests.get("https://www.yelp.com/search?
cflt=restaurants&find_loc=Hunts+Point%2C+Bronx%2C+NY+10474")
soup = BeautifulSoup (html_page.text, "html.parser")
TypeError: execute( ) takes from 2 to 3 positional arguments but 7 were
given
非常感谢您的帮助!
答案 0 :(得分:0)
您通常不需要在%s周围加上引号,因为模块会对此进行标记化并为您处理:
def store (name, rating, phone, address, link):
#The error is pointing to the following line:
cur.execute('INSERT INTO HuntsPointYelp (name, rating, phone, address,
link) VALUES (%s,%s,%s,%s,%s)',
name, rating, phone, address, link)
cur.connection.commit()